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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.866   ! raeburn     4: # $Id: lonnet.pm,v 1.865 2007/04/04 18:01:11 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.15      www        34: use HTTP::Headers;
1.486     www        35: use HTTP::Date;
                     36: # use Date::Parse;
1.11      www        37: use vars 
1.847     albertel   38: qw(%perlvar %badServerCache %spareid 
1.845     albertel   39:    %pr %prp $memcache %packagetab 
1.662     raeburn    40:    %courselogs %accesshash %userrolehash %domainrolehash $processmarker $dumpcount 
1.741     raeburn    41:    %coursedombuf %coursenumbuf %coursehombuf %coursedescrbuf %courseinstcodebuf %courseownerbuf %coursetypebuf
1.685     raeburn    42:    $tmpdir $_64bit %env);
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.637     raeburn    47: use HTML::Parser;
1.88      www        48: use Fcntl qw(:flock);
1.557     albertel   49: use Storable qw(lock_store lock_nstore lock_retrieve freeze thaw nfreeze);
1.539     albertel   50: use Time::HiRes qw( gettimeofday tv_interval );
1.599     albertel   51: use Cache::Memcached;
1.676     albertel   52: use Digest::MD5;
1.790     albertel   53: use Math::Random;
1.807     albertel   54: use LONCAPA qw(:DEFAULT :match);
1.740     www        55: use LONCAPA::Configuration;
1.676     albertel   56: 
1.195     www        57: my $readit;
1.550     foxr       58: my $max_connection_retries = 10;     # Or some such value.
1.1       albertel   59: 
1.619     albertel   60: require Exporter;
                     61: 
                     62: our @ISA = qw (Exporter);
                     63: our @EXPORT = qw(%env);
                     64: 
1.449     matthew    65: =pod
                     66: 
                     67: =head1 Package Variables
                     68: 
                     69: These are largely undocumented, so if you decipher one please note it here.
                     70: 
                     71: =over 4
                     72: 
                     73: =item $processmarker
                     74: 
                     75: Contains the time this process was started and this servers host id.
                     76: 
                     77: =item $dumpcount
                     78: 
                     79: Counts the number of times a message log flush has been attempted (regardless
                     80: of success) by this process.  Used as part of the filename when messages are
                     81: delayed.
                     82: 
                     83: =back
                     84: 
                     85: =cut
                     86: 
                     87: 
1.1       albertel   88: # --------------------------------------------------------------------- Logging
1.729     www        89: {
                     90:     my $logid;
                     91:     sub instructor_log {
                     92: 	my ($hash_name,$storehash,$delflag,$uname,$udom)=@_;
                     93: 	$logid++;
                     94: 	my $id=time().'00000'.$$.'00000'.$logid;
                     95: 	return &Apache::lonnet::put('nohist_'.$hash_name,
1.730     www        96: 				    { $id => {
                     97: 					'exe_uname' => $env{'user.name'},
                     98: 					'exe_udom'  => $env{'user.domain'},
                     99: 					'exe_time'  => time(),
                    100: 					'exe_ip'    => $ENV{'REMOTE_ADDR'},
                    101: 					'delflag'   => $delflag,
                    102: 					'logentry'  => $storehash,
                    103: 					'uname'     => $uname,
                    104: 					'udom'      => $udom,
                    105: 				    }
                    106: 				  },
1.729     www       107: 				    $env{'course.'.$env{'request.course.id'}.'.domain'},
                    108: 				    $env{'course.'.$env{'request.course.id'}.'.num'}
                    109: 				    );
                    110:     }
                    111: }
1.1       albertel  112: 
1.163     harris41  113: sub logtouch {
                    114:     my $execdir=$perlvar{'lonDaemons'};
1.448     albertel  115:     unless (-e "$execdir/logs/lonnet.log") {	
                    116: 	open(my $fh,">>$execdir/logs/lonnet.log");
1.163     harris41  117: 	close $fh;
                    118:     }
                    119:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                    120:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                    121: }
                    122: 
1.1       albertel  123: sub logthis {
                    124:     my $message=shift;
                    125:     my $execdir=$perlvar{'lonDaemons'};
                    126:     my $now=time;
                    127:     my $local=localtime($now);
1.448     albertel  128:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
                    129: 	print $fh "$local ($$): $message\n";
                    130: 	close($fh);
                    131:     }
1.1       albertel  132:     return 1;
                    133: }
                    134: 
                    135: sub logperm {
                    136:     my $message=shift;
                    137:     my $execdir=$perlvar{'lonDaemons'};
                    138:     my $now=time;
                    139:     my $local=localtime($now);
1.448     albertel  140:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
                    141: 	print $fh "$now:$message:$local\n";
                    142: 	close($fh);
                    143:     }
1.1       albertel  144:     return 1;
                    145: }
                    146: 
1.850     albertel  147: sub create_connection {
1.853     albertel  148:     my ($hostname,$lonid) = @_;
1.851     albertel  149:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
1.850     albertel  150: 				     Type    => SOCK_STREAM,
                    151: 				     Timeout => 10);
                    152:     return 0 if (!$client);
1.854     albertel  153:     print $client (join(':',$hostname,$lonid,&machine_ids($lonid))."\n");
1.850     albertel  154:     my $result = <$client>;
                    155:     chomp($result);
                    156:     return 1 if ($result eq 'done');
                    157:     return 0;
                    158: }
                    159: 
                    160: 
1.1       albertel  161: # -------------------------------------------------- Non-critical communication
                    162: sub subreply {
                    163:     my ($cmd,$server)=@_;
1.838     albertel  164:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      165:     #
                    166:     #  With loncnew process trimming, there's a timing hole between lonc server
                    167:     #  process exit and the master server picking up the listen on the AF_UNIX
                    168:     #  socket.  In that time interval, a lock file will exist:
                    169: 
                    170:     my $lockfile=$peerfile.".lock";
                    171:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
                    172: 	sleep(1);
                    173:     }
                    174:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      175:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      176:     #
1.550     foxr      177:     #   We'll give the connection a few tries before abandoning it.  If
                    178:     #   connection is not possible, we'll con_lost back to the client.
                    179:     #   
                    180:     my $client;
                    181:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    182: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    183: 				      Type    => SOCK_STREAM,
                    184: 				      Timeout => 10);
                    185: 	if($client) {
                    186: 	    last;		# Connected!
1.850     albertel  187: 	} else {
1.853     albertel  188: 	    &create_connection(&hostname($server),$server);
1.550     foxr      189: 	}
1.850     albertel  190:         sleep(1);		# Try again later if failed connection.
1.550     foxr      191:     }
                    192:     my $answer;
                    193:     if ($client) {
1.704     albertel  194: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      195: 	$answer=<$client>;
                    196: 	if (!$answer) { $answer="con_lost"; }
                    197: 	chomp($answer);
                    198:     } else {
                    199: 	$answer = 'con_lost';	# Failed connection.
                    200:     }
1.1       albertel  201:     return $answer;
                    202: }
                    203: 
                    204: sub reply {
                    205:     my ($cmd,$server)=@_;
1.838     albertel  206:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  207:     my $answer=subreply($cmd,$server);
1.65      www       208:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672     albertel  209:        &logthis("<font color=\"blue\">WARNING:".
1.12      www       210:                 " $cmd to $server returned $answer</font>");
                    211:     }
1.1       albertel  212:     return $answer;
                    213: }
                    214: 
                    215: # ----------------------------------------------------------- Send USR1 to lonc
                    216: 
                    217: sub reconlonc {
1.836     www       218:     &logthis("Trying to reconnect lonc");
1.1       albertel  219:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448     albertel  220:     if (open(my $fh,"<$loncfile")) {
1.1       albertel  221: 	my $loncpid=<$fh>;
                    222:         chomp($loncpid);
                    223:         if (kill 0 => $loncpid) {
                    224: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    225:             kill USR1 => $loncpid;
                    226:             sleep 1;
1.836     www       227:          } else {
1.12      www       228: 	    &logthis(
1.672     albertel  229:                "<font color=\"blue\">WARNING:".
1.12      www       230:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  231:         }
                    232:     } else {
1.836     www       233: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  234:     }
                    235: }
                    236: 
                    237: # ------------------------------------------------------ Critical communication
1.12      www       238: 
1.1       albertel  239: sub critical {
                    240:     my ($cmd,$server)=@_;
1.838     albertel  241:     unless (&hostname($server)) {
1.672     albertel  242:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       243:                " Critical message to unknown server ($server)</font>");
                    244:         return 'no_such_host';
                    245:     }
1.1       albertel  246:     my $answer=reply($cmd,$server);
                    247:     if ($answer eq 'con_lost') {
                    248: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
1.589     albertel  249: 	my $answer=reply($cmd,$server);
1.1       albertel  250:         if ($answer eq 'con_lost') {
                    251:             my $now=time;
                    252:             my $middlename=$cmd;
1.5       www       253:             $middlename=substr($middlename,0,16);
1.1       albertel  254:             $middlename=~s/\W//g;
                    255:             my $dfilename=
1.305     www       256:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    257:             $dumpcount++;
1.1       albertel  258:             {
1.448     albertel  259: 		my $dfh;
                    260: 		if (open($dfh,">$dfilename")) {
                    261: 		    print $dfh "$cmd\n"; 
                    262: 		    close($dfh);
                    263: 		}
1.1       albertel  264:             }
                    265:             sleep 2;
                    266:             my $wcmd='';
                    267:             {
1.448     albertel  268: 		my $dfh;
                    269: 		if (open($dfh,"<$dfilename")) {
                    270: 		    $wcmd=<$dfh>; 
                    271: 		    close($dfh);
                    272: 		}
1.1       albertel  273:             }
                    274:             chomp($wcmd);
1.7       www       275:             if ($wcmd eq $cmd) {
1.672     albertel  276: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       277:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  278:                 &logperm("D:$server:$cmd");
                    279: 	        return 'con_delayed';
                    280:             } else {
1.672     albertel  281:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       282:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  283:                 &logperm("F:$server:$cmd");
                    284:                 return 'con_failed';
                    285:             }
                    286:         }
                    287:     }
                    288:     return $answer;
1.405     albertel  289: }
                    290: 
1.755     albertel  291: # ------------------------------------------- check if return value is an error
                    292: 
                    293: sub error {
                    294:     my ($result) = @_;
1.756     albertel  295:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  296: 	if ($2 == 2) { return undef; }
                    297: 	return $1;
                    298:     }
                    299:     return undef;
                    300: }
                    301: 
1.783     albertel  302: sub convert_and_load_session_env {
                    303:     my ($lonidsdir,$handle)=@_;
                    304:     my @profile;
                    305:     {
                    306: 	open(my $idf,"$lonidsdir/$handle.id");
                    307: 	flock($idf,LOCK_SH);
                    308: 	@profile=<$idf>;
                    309: 	close($idf);
                    310:     }
                    311:     my %temp_env;
                    312:     foreach my $line (@profile) {
1.786     albertel  313: 	if ($line !~ m/=/) {
                    314: 	    return 0;
                    315: 	}
1.783     albertel  316: 	chomp($line);
                    317: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    318: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    319:     }
                    320:     unlink("$lonidsdir/$handle.id");
                    321:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    322: 	    0640)) {
                    323: 	%disk_env = %temp_env;
                    324: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    325: 	untie(%disk_env);
                    326:     }
1.786     albertel  327:     return 1;
1.783     albertel  328: }
                    329: 
1.374     www       330: # ------------------------------------------- Transfer profile into environment
1.780     albertel  331: my $env_loaded;
                    332: sub transfer_profile_to_env {
1.788     albertel  333:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    334:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       335: 
1.720     albertel  336:     if (!defined($lonidsdir)) {
                    337: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    338:     }
                    339:     if (!defined($handle)) {
                    340:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    341:     }
                    342: 
1.786     albertel  343:     my $convert;
                    344:     {
                    345:     	open(my $idf,"$lonidsdir/$handle.id");
                    346: 	flock($idf,LOCK_SH);
                    347: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    348: 		&GDBM_READER(),0640)) {
                    349: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    350: 	    untie(%disk_env);
                    351: 	} else {
                    352: 	    $convert = 1;
                    353: 	}
                    354:     }
                    355:     if ($convert) {
                    356: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    357: 	    &logthis("Failed to load session, or convert session.");
                    358: 	}
1.374     www       359:     }
1.783     albertel  360: 
1.786     albertel  361:     my %remove;
1.783     albertel  362:     while ( my $envname = each(%env) ) {
1.433     matthew   363:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    364:             if ($time < time-300) {
1.783     albertel  365:                 $remove{$key}++;
1.433     matthew   366:             }
                    367:         }
                    368:     }
1.783     albertel  369: 
1.619     albertel  370:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  371:     $env_loaded=1;
1.783     albertel  372:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   373:         &delenv($expired_key);
1.374     www       374:     }
1.1       albertel  375: }
                    376: 
1.830     albertel  377: sub timed_flock {
                    378:     my ($file,$lock_type) = @_;
                    379:     my $failed=0;
                    380:     eval {
                    381: 	local $SIG{__DIE__}='DEFAULT';
                    382: 	local $SIG{ALRM}=sub {
                    383: 	    $failed=1;
                    384: 	    die("failed lock");
                    385: 	};
                    386: 	alarm(13);
                    387: 	flock($file,$lock_type);
                    388: 	alarm(0);
                    389:     };
                    390:     if ($failed) {
                    391: 	return undef;
                    392:     } else {
                    393: 	return 1;
                    394:     }
                    395: }
                    396: 
1.5       www       397: # ---------------------------------------------------------- Append Environment
                    398: 
                    399: sub appenv {
1.6       www       400:     my %newenv=@_;
1.692     albertel  401:     foreach my $key (keys(%newenv)) {
                    402: 	if (($newenv{$key}=~/^user\.role/) || ($newenv{$key}=~/^user\.priv/)) {
1.672     albertel  403:             &logthis("<font color=\"blue\">WARNING: ".
1.692     albertel  404:                 "Attempt to modify environment ".$key." to ".$newenv{$key}
1.151     www       405:                 .'</font>');
1.692     albertel  406: 	    delete($newenv{$key});
1.35      www       407:         } else {
1.692     albertel  408:             $env{$key}=$newenv{$key};
1.35      www       409:         }
1.191     harris41  410:     }
1.830     albertel  411:     open(my $env_file,$env{'user.environment'});
                    412:     if (&timed_flock($env_file,LOCK_EX)
                    413: 	&&
                    414: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    415: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  416: 	while (my ($key,$value) = each(%newenv)) {
                    417: 	    $disk_env{$key} = $value;
1.448     albertel  418: 	}
1.783     albertel  419: 	untie(%disk_env);
1.56      www       420:     }
                    421:     return 'ok';
                    422: }
                    423: # ----------------------------------------------------- Delete from Environment
                    424: 
                    425: sub delenv {
                    426:     my $delthis=shift;
                    427:     if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
1.672     albertel  428:         &logthis("<font color=\"blue\">WARNING: ".
1.56      www       429:                 "Attempt to delete from environment ".$delthis);
                    430:         return 'error';
                    431:     }
1.830     albertel  432:     open(my $env_file,$env{'user.environment'});
                    433:     if (&timed_flock($env_file,LOCK_EX)
                    434: 	&&
                    435: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    436: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  437: 	foreach my $key (keys(%disk_env)) {
                    438: 	    if ($key=~/^$delthis/) { 
1.619     albertel  439:                 delete($env{$key});
1.783     albertel  440:                 delete($disk_env{$key});
1.473     matthew   441:             }
1.448     albertel  442: 	}
1.783     albertel  443: 	untie(%disk_env);
1.5       www       444:     }
                    445:     return 'ok';
1.369     albertel  446: }
                    447: 
1.790     albertel  448: sub get_env_multiple {
                    449:     my ($name) = @_;
                    450:     my @values;
                    451:     if (defined($env{$name})) {
                    452:         # exists is it an array
                    453:         if (ref($env{$name})) {
                    454:             @values=@{ $env{$name} };
                    455:         } else {
                    456:             $values[0]=$env{$name};
                    457:         }
                    458:     }
                    459:     return(@values);
                    460: }
                    461: 
1.369     albertel  462: # ------------------------------------------ Find out current server userload
                    463: # there is a copy in lond
                    464: sub userload {
                    465:     my $numusers=0;
                    466:     {
                    467: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                    468: 	my $filename;
                    469: 	my $curtime=time;
                    470: 	while ($filename=readdir(LONIDS)) {
                    471: 	    if ($filename eq '.' || $filename eq '..') {next;}
1.404     albertel  472: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel  473: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel  474: 	}
                    475: 	closedir(LONIDS);
                    476:     }
                    477:     my $userloadpercent=0;
                    478:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                    479:     if ($maxuserload) {
1.371     albertel  480: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel  481:     }
1.372     albertel  482:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel  483:     return $userloadpercent;
1.283     www       484: }
                    485: 
                    486: # ------------------------------------------ Fight off request when overloaded
                    487: 
                    488: sub overloaderror {
                    489:     my ($r,$checkserver)=@_;
                    490:     unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
                    491:     my $loadavg;
                    492:     if ($checkserver eq $perlvar{'lonHostID'}) {
1.448     albertel  493:        open(my $loadfile,'/proc/loadavg');
1.283     www       494:        $loadavg=<$loadfile>;
                    495:        $loadavg =~ s/\s.*//g;
1.285     matthew   496:        $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.448     albertel  497:        close($loadfile);
1.283     www       498:     } else {
                    499:        $loadavg=&reply('load',$checkserver);
                    500:     }
1.285     matthew   501:     my $overload=$loadavg-100;
1.283     www       502:     if ($overload>0) {
1.285     matthew   503: 	$r->err_headers_out->{'Retry-After'}=$overload;
1.283     www       504:         $r->log_error('Overload of '.$overload.' on '.$checkserver);
1.554     www       505:         return 413;
1.283     www       506:     }    
                    507:     return '';
1.5       www       508: }
1.1       albertel  509: 
                    510: # ------------------------------ Find server with least workload from spare.tab
1.11      www       511: 
1.1       albertel  512: sub spareserver {
1.670     albertel  513:     my ($loadpercent,$userloadpercent,$want_server_name) = @_;
1.784     albertel  514:     my $spare_server;
1.370     albertel  515:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel  516:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                    517:                                                      :  $userloadpercent;
                    518:     
                    519:     foreach my $try_server (@{ $spareid{'primary'} }) {
                    520: 	($spare_server, $lowest_load) =
                    521: 	    &compare_server_load($try_server, $spare_server, $lowest_load);
                    522:     }
                    523: 
                    524:     my $found_server = ($spare_server ne '' && $lowest_load < 100);
                    525: 
                    526:     if (!$found_server) {
                    527: 	foreach my $try_server (@{ $spareid{'default'} }) {
                    528: 	    ($spare_server, $lowest_load) =
                    529: 		&compare_server_load($try_server, $spare_server, $lowest_load);
                    530: 	}
                    531:     }
                    532: 
                    533:     if (!$want_server_name) {
1.838     albertel  534: 	$spare_server="http://".&hostname($spare_server);
1.784     albertel  535:     }
                    536:     return $spare_server;
                    537: }
                    538: 
                    539: sub compare_server_load {
                    540:     my ($try_server, $spare_server, $lowest_load) = @_;
                    541: 
                    542:     my $loadans     = &reply('load',    $try_server);
                    543:     my $userloadans = &reply('userload',$try_server);
                    544: 
                    545:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
                    546: 	next; #didn't get a number from the server
                    547:     }
                    548: 
                    549:     my $load;
                    550:     if ($loadans =~ /\d/) {
                    551: 	if ($userloadans =~ /\d/) {
                    552: 	    #both are numbers, pick the bigger one
                    553: 	    $load = ($loadans > $userloadans) ? $loadans 
                    554: 		                              : $userloadans;
1.411     albertel  555: 	} else {
1.784     albertel  556: 	    $load = $loadans;
1.411     albertel  557: 	}
1.784     albertel  558:     } else {
                    559: 	$load = $userloadans;
                    560:     }
                    561: 
                    562:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                    563: 	$spare_server = $try_server;
                    564: 	$lowest_load  = $load;
1.370     albertel  565:     }
1.784     albertel  566:     return ($spare_server,$lowest_load);
1.202     matthew   567: }
                    568: # --------------------------------------------- Try to change a user's password
                    569: 
                    570: sub changepass {
1.799     raeburn   571:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew   572:     $currentpass = &escape($currentpass);
                    573:     $newpass     = &escape($newpass);
1.799     raeburn   574:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context",
1.202     matthew   575: 		       $server);
                    576:     if (! $answer) {
                    577: 	&logthis("No reply on password change request to $server ".
                    578: 		 "by $uname in domain $udom.");
                    579:     } elsif ($answer =~ "^ok") {
                    580:         &logthis("$uname in $udom successfully changed their password ".
                    581: 		 "on $server.");
                    582:     } elsif ($answer =~ "^pwchange_failure") {
                    583: 	&logthis("$uname in $udom was unable to change their password ".
                    584: 		 "on $server.  The action was blocked by either lcpasswd ".
                    585: 		 "or pwchange");
                    586:     } elsif ($answer =~ "^non_authorized") {
                    587:         &logthis("$uname in $udom did not get their password correct when ".
                    588: 		 "attempting to change it on $server.");
                    589:     } elsif ($answer =~ "^auth_mode_error") {
                    590:         &logthis("$uname in $udom attempted to change their password despite ".
                    591: 		 "not being locally or internally authenticated on $server.");
                    592:     } elsif ($answer =~ "^unknown_user") {
                    593:         &logthis("$uname in $udom attempted to change their password ".
                    594: 		 "on $server but were unable to because $server is not ".
                    595: 		 "their home server.");
                    596:     } elsif ($answer =~ "^refused") {
                    597: 	&logthis("$server refused to change $uname in $udom password because ".
                    598: 		 "it was sent an unencrypted request to change the password.");
                    599:     }
                    600:     return $answer;
1.1       albertel  601: }
                    602: 
1.169     harris41  603: # ----------------------- Try to determine user's current authentication scheme
                    604: 
                    605: sub queryauthenticate {
                    606:     my ($uname,$udom)=@_;
1.456     albertel  607:     my $uhome=&homeserver($uname,$udom);
                    608:     if (!$uhome) {
                    609: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                    610: 	return 'no_host';
                    611:     }
                    612:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                    613:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                    614: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41  615:     }
1.456     albertel  616:     return $answer;
1.169     harris41  617: }
                    618: 
1.1       albertel  619: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www       620: 
1.1       albertel  621: sub authenticate {
                    622:     my ($uname,$upass,$udom)=@_;
1.807     albertel  623:     $upass=&escape($upass);
                    624:     $uname= &LONCAPA::clean_username($uname);
1.836     www       625:     my $uhome=&homeserver($uname,$udom,1);
                    626:     if ((!$uhome) || ($uhome eq 'no_host')) {
                    627: # Maybe the machine was offline and only re-appeared again recently?
                    628:         &reconlonc();
                    629: # One more
                    630: 	my $uhome=&homeserver($uname,$udom,1);
                    631: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                    632: 	    &logthis("User $uname at $udom is unknown in authenticate");
                    633: 	}
1.471     albertel  634: 	return 'no_host';
1.1       albertel  635:     }
1.471     albertel  636:     my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
                    637:     if ($answer eq 'authorized') {
                    638: 	&logthis("User $uname at $udom authorized by $uhome"); 
                    639: 	return $uhome; 
                    640:     }
                    641:     if ($answer eq 'non_authorized') {
                    642: 	&logthis("User $uname at $udom rejected by $uhome");
                    643: 	return 'no_host'; 
1.9       www       644:     }
1.471     albertel  645:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel  646:     return 'no_host';
                    647: }
                    648: 
                    649: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www       650: 
1.599     albertel  651: my %homecache;
1.1       albertel  652: sub homeserver {
1.230     stredwic  653:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel  654:     my $index="$uname:$udom";
1.426     albertel  655: 
1.599     albertel  656:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel  657: 
                    658:     my %servers = &get_servers($udom,'library');
                    659:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic  660:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic  661: 		 exists($badServerCache{$tryserver}));
1.841     albertel  662: 
                    663: 	my $answer=reply("home:$udom:$uname",$tryserver);
                    664: 	if ($answer eq 'found') {
                    665: 	    delete($badServerCache{$tryserver}); 
                    666: 	    return $homecache{$index}=$tryserver;
                    667: 	} elsif ($answer eq 'no_host') {
                    668: 	    $badServerCache{$tryserver}=1;
                    669: 	}
1.1       albertel  670:     }    
                    671:     return 'no_host';
1.70      www       672: }
                    673: 
                    674: # ------------------------------------- Find the usernames behind a list of IDs
                    675: 
                    676: sub idget {
                    677:     my ($udom,@ids)=@_;
                    678:     my %returnhash=();
                    679:     
1.841     albertel  680:     my %servers = &get_servers($udom,'library');
                    681:     foreach my $tryserver (keys(%servers)) {
                    682: 	my $idlist=join('&',@ids);
                    683: 	$idlist=~tr/A-Z/a-z/; 
                    684: 	my $reply=&reply("idget:$udom:".$idlist,$tryserver);
                    685: 	my @answer=();
                    686: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                    687: 	    @answer=split(/\&/,$reply);
                    688: 	}                    ;
                    689: 	my $i;
                    690: 	for ($i=0;$i<=$#ids;$i++) {
                    691: 	    if ($answer[$i]) {
                    692: 		$returnhash{$ids[$i]}=$answer[$i];
                    693: 	    } 
                    694: 	}
                    695:     } 
1.70      www       696:     return %returnhash;
                    697: }
                    698: 
                    699: # ------------------------------------- Find the IDs behind a list of usernames
                    700: 
                    701: sub idrget {
                    702:     my ($udom,@unames)=@_;
                    703:     my %returnhash=();
1.800     albertel  704:     foreach my $uname (@unames) {
                    705:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41  706:     }
1.70      www       707:     return %returnhash;
                    708: }
                    709: 
                    710: # ------------------------------- Store away a list of names and associated IDs
                    711: 
                    712: sub idput {
                    713:     my ($udom,%ids)=@_;
                    714:     my %servers=();
1.800     albertel  715:     foreach my $uname (keys(%ids)) {
                    716: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
                    717:         my $uhom=&homeserver($uname,$udom);
1.70      www       718:         if ($uhom ne 'no_host') {
1.800     albertel  719:             my $id=&escape($ids{$uname});
1.70      www       720:             $id=~tr/A-Z/a-z/;
1.800     albertel  721:             my $esc_unam=&escape($uname);
1.70      www       722: 	    if ($servers{$uhom}) {
1.800     albertel  723: 		$servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70      www       724:             } else {
1.800     albertel  725:                 $servers{$uhom}=$id.'='.$esc_unam;
1.70      www       726:             }
                    727:         }
1.191     harris41  728:     }
1.800     albertel  729:     foreach my $server (keys(%servers)) {
                    730:         &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191     harris41  731:     }
1.344     www       732: }
                    733: 
1.806     raeburn   734: # ------------------------------------------- get items from domain db files   
                    735: 
                    736: sub get_dom {
1.860     raeburn   737:     my ($namespace,$storearr,$udom,$uhome)=@_;
1.806     raeburn   738:     my $items='';
                    739:     foreach my $item (@$storearr) {
                    740:         $items.=&escape($item).'&';
                    741:     }
                    742:     $items=~s/\&$//;
1.860     raeburn   743:     if (!$udom) {
                    744:         $udom=$env{'user.domain'};
                    745:         if (defined(&domain($udom,'primary'))) {
                    746:             $uhome=&domain($udom,'primary');
                    747:         } else {
                    748:             $uhome eq '';
                    749:         }
                    750:     } else {
                    751:         if (!$uhome) {
                    752:             if (defined(&domain($udom,'primary'))) {
                    753:                 $uhome=&domain($udom,'primary');
                    754:             }
                    755:         }
                    756:     }
                    757:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn   758:         my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866   ! raeburn   759:         my %returnhash;
        !           760:         if ($rep =~ /^error: 2 /) {
        !           761:             return %returnhash;
        !           762:         }
1.806     raeburn   763:         my @pairs=split(/\&/,$rep);
                    764:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                    765:             return @pairs;
                    766:         }
                    767:         my %returnhash=();
                    768:         my $i=0;
                    769:         foreach my $item (@$storearr) {
                    770:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                    771:             $i++;
                    772:         }
                    773:         return %returnhash;
                    774:     } else {
1.860     raeburn   775:         &logthis("get_dom failed - no homeserver and/or domain");
1.806     raeburn   776:     }
                    777: }
                    778: 
                    779: # -------------------------------------------- put items in domain db files 
                    780: 
                    781: sub put_dom {
1.860     raeburn   782:     my ($namespace,$storehash,$udom,$uhome)=@_;
                    783:     if (!$udom) {
                    784:         $udom=$env{'user.domain'};
                    785:         if (defined(&domain($udom,'primary'))) {
                    786:             $uhome=&domain($udom,'primary');
                    787:         } else {
                    788:             $uhome eq '';
                    789:         }
                    790:     } else {
                    791:         if (!$uhome) {
                    792:             if (defined(&domain($udom,'primary'))) {
                    793:                 $uhome=&domain($udom,'primary');
                    794:             }
                    795:         }
                    796:     } 
                    797:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn   798:         my $items='';
                    799:         foreach my $item (keys(%$storehash)) {
                    800:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                    801:         }
                    802:         $items=~s/\&$//;
                    803:         return &reply("putdom:$udom:$namespace:$items",$uhome);
                    804:     } else {
1.860     raeburn   805:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn   806:     }
                    807: }
                    808: 
1.837     raeburn   809: sub retrieve_inst_usertypes {
                    810:     my ($udom) = @_;
                    811:     my (%returnhash,@order);
1.846     albertel  812:     if (defined(&domain($udom,'primary'))) {
                    813:         my $uhome=&domain($udom,'primary');
1.837     raeburn   814:         my $rep=&reply("inst_usertypes:$udom",$uhome);
                    815:         my ($hashitems,$orderitems) = split(/:/,$rep); 
                    816:         my @pairs=split(/\&/,$hashitems);
                    817:         foreach my $item (@pairs) {
                    818:             my ($key,$value)=split(/=/,$item,2);
                    819:             $key = &unescape($key);
                    820:             next if ($key =~ /^error: 2 /);
                    821:             $returnhash{$key}=&thaw_unescape($value);
                    822:         }
                    823:         my @esc_order = split(/\&/,$orderitems);
                    824:         foreach my $item (@esc_order) {
                    825:             push(@order,&unescape($item));
                    826:         }
                    827:     } else {
                    828:         &logthis("get_dom failed - no primary domain server for $udom");
                    829:     }
                    830:     return (\%returnhash,\@order);
                    831: }
                    832: 
1.344     www       833: # --------------------------------------------------- Assign a key to a student
                    834: 
                    835: sub assign_access_key {
1.364     www       836: #
                    837: # a valid key looks like uname:udom#comments
                    838: # comments are being appended
                    839: #
1.498     www       840:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                    841:     $kdom=
1.620     albertel  842:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www       843:     $knum=
1.620     albertel  844:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www       845:     $cdom=
1.620     albertel  846:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www       847:     $cnum=
1.620     albertel  848:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                    849:     $udom=$env{'user.name'} unless (defined($udom));
                    850:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www       851:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www       852:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel  853:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www       854:                                                   # assigned to this person
                    855:                                                   # - this should not happen,
1.345     www       856:                                                   # unless something went wrong
                    857:                                                   # the first time around
                    858: # ready to assign
1.364     www       859:         $logentry=$1.'; '.$logentry;
1.496     www       860:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www       861:                                                  $kdom,$knum) eq 'ok') {
1.345     www       862: # key now belongs to user
1.346     www       863: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www       864:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
                    865:                 &appenv('environment.'.$envkey => $ckey);
                    866:                 return 'ok';
                    867:             } else {
                    868:                 return 
                    869:   'error: Count not permanently assign key, will need to be re-entered later.';
                    870: 	    }
                    871:         } else {
                    872:             return 'error: Could not assign key, try again later.';
                    873:         }
1.364     www       874:     } elsif (!$existing{$ckey}) {
1.345     www       875: # the key does not exist
                    876: 	return 'error: The key does not exist';
                    877:     } else {
                    878: # the key is somebody else's
                    879: 	return 'error: The key is already in use';
                    880:     }
1.344     www       881: }
                    882: 
1.364     www       883: # ------------------------------------------ put an additional comment on a key
                    884: 
                    885: sub comment_access_key {
                    886: #
                    887: # a valid key looks like uname:udom#comments
                    888: # comments are being appended
                    889: #
                    890:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                    891:     $cdom=
1.620     albertel  892:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www       893:     $cnum=
1.620     albertel  894:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www       895:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                    896:     if ($existing{$ckey}) {
                    897:         $existing{$ckey}.='; '.$logentry;
                    898: # ready to assign
1.367     www       899:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www       900:                                                  $cdom,$cnum) eq 'ok') {
                    901: 	    return 'ok';
                    902:         } else {
                    903: 	    return 'error: Count not store comment.';
                    904:         }
                    905:     } else {
                    906: # the key does not exist
                    907: 	return 'error: The key does not exist';
                    908:     }
                    909: }
                    910: 
1.344     www       911: # ------------------------------------------------------ Generate a set of keys
                    912: 
                    913: sub generate_access_keys {
1.364     www       914:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www       915:     $cdom=
1.620     albertel  916:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www       917:     $cnum=
1.620     albertel  918:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www       919:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www       920:     unless (($cdom) && ($cnum)) { return 0; }
                    921:     if ($number>10000) { return 0; }
                    922:     sleep(2); # make sure don't get same seed twice
                    923:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                    924:     my $total=0;
                    925:     for (my $i=1;$i<=$number;$i++) {
                    926:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                    927:                   sprintf("%lx",int(100000*rand)).'-'.
                    928:                   sprintf("%lx",int(100000*rand));
                    929:        $newkey=~s/1/g/g; # folks mix up 1 and l
                    930:        $newkey=~s/0/h/g; # and also 0 and O
                    931:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                    932:        if ($existing{$newkey}) {
                    933:            $i--;
                    934:        } else {
1.364     www       935: 	  if (&put('accesskeys',
                    936:               { $newkey => '# generated '.localtime().
1.620     albertel  937:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www       938:                            '; '.$logentry },
                    939: 		   $cdom,$cnum) eq 'ok') {
1.344     www       940:               $total++;
                    941: 	  }
                    942:        }
                    943:     }
1.620     albertel  944:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www       945:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                    946:     return $total;
                    947: }
                    948: 
                    949: # ------------------------------------------------------- Validate an accesskey
                    950: 
                    951: sub validate_access_key {
                    952:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                    953:     $cdom=
1.620     albertel  954:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www       955:     $cnum=
1.620     albertel  956:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                    957:     $udom=$env{'user.domain'} unless (defined($udom));
                    958:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www       959:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel  960:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www       961: }
                    962: 
                    963: # ------------------------------------- Find the section of student in a course
1.652     albertel  964: sub devalidate_getsection_cache {
                    965:     my ($udom,$unam,$courseid)=@_;
                    966:     my $hashid="$udom:$unam:$courseid";
                    967:     &devalidate_cache_new('getsection',$hashid);
                    968: }
1.298     matthew   969: 
1.815     albertel  970: sub courseid_to_courseurl {
                    971:     my ($courseid) = @_;
                    972:     #already url style courseid
                    973:     return $courseid if ($courseid =~ m{^/});
                    974: 
                    975:     if (exists($env{'course.'.$courseid.'.num'})) {
                    976: 	my $cnum = $env{'course.'.$courseid.'.num'};
                    977: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                    978: 	return "/$cdom/$cnum";
                    979:     }
                    980: 
                    981:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
                    982:     if (exists($courseinfo{'num'})) {
                    983: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                    984:     }
                    985: 
                    986:     return undef;
                    987: }
                    988: 
1.298     matthew   989: sub getsection {
                    990:     my ($udom,$unam,$courseid)=@_;
1.599     albertel  991:     my $cachetime=1800;
1.551     albertel  992: 
                    993:     my $hashid="$udom:$unam:$courseid";
1.599     albertel  994:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel  995:     if (defined($cached)) { return $result; }
                    996: 
1.298     matthew   997:     my %Pending; 
                    998:     my %Expired;
                    999:     #
                   1000:     # Each role can either have not started yet (pending), be active, 
                   1001:     #    or have expired.
                   1002:     #
                   1003:     # If there is an active role, we are done.
                   1004:     #
                   1005:     # If there is more than one role which has not started yet, 
                   1006:     #     choose the one which will start sooner
                   1007:     # If there is one role which has not started yet, return it.
                   1008:     #
                   1009:     # If there is more than one expired role, choose the one which ended last.
                   1010:     # If there is a role which has expired, return it.
                   1011:     #
1.815     albertel 1012:     $courseid = &courseid_to_courseurl($courseid);
1.817     raeburn  1013:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
                   1014:     foreach my $key (keys(%roleshash)) {
1.479     albertel 1015:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  1016:         my $section=$1;
                   1017:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  1018:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  1019:         my $now=time;
1.548     albertel 1020:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  1021:             $Expired{$end}=$section;
                   1022:             next;
                   1023:         }
1.548     albertel 1024:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  1025:             $Pending{$start}=$section;
                   1026:             next;
                   1027:         }
1.599     albertel 1028:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  1029:     }
                   1030:     #
                   1031:     # Presumedly there will be few matching roles from the above
                   1032:     # loop and the sorting time will be negligible.
                   1033:     if (scalar(keys(%Pending))) {
                   1034:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 1035:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  1036:     } 
                   1037:     if (scalar(keys(%Expired))) {
                   1038:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   1039:         my $time = pop(@sorted);
1.599     albertel 1040:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  1041:     }
1.599     albertel 1042:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  1043: }
1.70      www      1044: 
1.599     albertel 1045: sub save_cache {
                   1046:     &purge_remembered();
1.722     albertel 1047:     #&Apache::loncommon::validate_page();
1.620     albertel 1048:     undef(%env);
1.780     albertel 1049:     undef($env_loaded);
1.599     albertel 1050: }
1.452     albertel 1051: 
1.599     albertel 1052: my $to_remember=-1;
                   1053: my %remembered;
                   1054: my %accessed;
                   1055: my $kicks=0;
                   1056: my $hits=0;
1.849     albertel 1057: sub make_key {
                   1058:     my ($name,$id) = @_;
                   1059:     if (length($id) > 200) { $id=length($id).':'.&Digest::MD5::md5_hex($id); }
                   1060:     return &escape($name.':'.$id);
                   1061: }
                   1062: 
1.599     albertel 1063: sub devalidate_cache_new {
                   1064:     my ($name,$id,$debug) = @_;
                   1065:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849     albertel 1066:     $id=&make_key($name,$id);
1.599     albertel 1067:     $memcache->delete($id);
                   1068:     delete($remembered{$id});
                   1069:     delete($accessed{$id});
                   1070: }
                   1071: 
                   1072: sub is_cached_new {
                   1073:     my ($name,$id,$debug) = @_;
1.849     albertel 1074:     $id=&make_key($name,$id);
1.599     albertel 1075:     if (exists($remembered{$id})) {
                   1076: 	if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
                   1077: 	$accessed{$id}=[&gettimeofday()];
                   1078: 	$hits++;
                   1079: 	return ($remembered{$id},1);
                   1080:     }
                   1081:     my $value = $memcache->get($id);
                   1082:     if (!(defined($value))) {
                   1083: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 1084: 	return (undef,undef);
1.416     albertel 1085:     }
1.599     albertel 1086:     if ($value eq '__undef__') {
                   1087: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   1088: 	$value=undef;
                   1089:     }
                   1090:     &make_room($id,$value,$debug);
                   1091:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   1092:     return ($value,1);
                   1093: }
                   1094: 
                   1095: sub do_cache_new {
                   1096:     my ($name,$id,$value,$time,$debug) = @_;
1.849     albertel 1097:     $id=&make_key($name,$id);
1.599     albertel 1098:     my $setvalue=$value;
                   1099:     if (!defined($setvalue)) {
                   1100: 	$setvalue='__undef__';
                   1101:     }
1.623     albertel 1102:     if (!defined($time) ) {
                   1103: 	$time=600;
                   1104:     }
1.599     albertel 1105:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.600     albertel 1106:     $memcache->set($id,$setvalue,$time);
                   1107:     # need to make a copy of $value
                   1108:     #&make_room($id,$value,$debug);
1.599     albertel 1109:     return $value;
                   1110: }
                   1111: 
                   1112: sub make_room {
                   1113:     my ($id,$value,$debug)=@_;
                   1114:     $remembered{$id}=$value;
                   1115:     if ($to_remember<0) { return; }
                   1116:     $accessed{$id}=[&gettimeofday()];
                   1117:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   1118:     my $to_kick;
                   1119:     my $max_time=0;
                   1120:     foreach my $other (keys(%accessed)) {
                   1121: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   1122: 	    $to_kick=$other;
                   1123: 	    $max_time=&tv_interval($accessed{$other});
                   1124: 	}
                   1125:     }
                   1126:     delete($remembered{$to_kick});
                   1127:     delete($accessed{$to_kick});
                   1128:     $kicks++;
                   1129:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 1130:     return;
                   1131: }
                   1132: 
1.599     albertel 1133: sub purge_remembered {
1.604     albertel 1134:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   1135:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 1136:     undef(%remembered);
                   1137:     undef(%accessed);
1.428     albertel 1138: }
1.70      www      1139: # ------------------------------------- Read an entry from a user's environment
                   1140: 
                   1141: sub userenvironment {
                   1142:     my ($udom,$unam,@what)=@_;
                   1143:     my %returnhash=();
                   1144:     my @answer=split(/\&/,
                   1145:                 &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
                   1146:                       &homeserver($unam,$udom)));
                   1147:     my $i;
                   1148:     for ($i=0;$i<=$#what;$i++) {
                   1149: 	$returnhash{$what[$i]}=&unescape($answer[$i]);
                   1150:     }
                   1151:     return %returnhash;
1.1       albertel 1152: }
                   1153: 
1.617     albertel 1154: # ---------------------------------------------------------- Get a studentphoto
                   1155: sub studentphoto {
                   1156:     my ($udom,$unam,$ext) = @_;
                   1157:     my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706     raeburn  1158:     if (defined($env{'request.course.id'})) {
1.708     raeburn  1159:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  1160:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   1161:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   1162:             } else {
                   1163:                 my ($result,$perm_reqd)=
1.707     albertel 1164: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1165:                 if ($result eq 'ok') {
                   1166:                     if (!($perm_reqd eq 'yes')) {
                   1167:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   1168:                     }
                   1169:                 }
                   1170:             }
                   1171:         }
                   1172:     } else {
                   1173:         my ($result,$perm_reqd) = 
1.707     albertel 1174: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1175:         if ($result eq 'ok') {
                   1176:             if (!($perm_reqd eq 'yes')) {
                   1177:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   1178:             }
                   1179:         }
                   1180:     }
                   1181:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   1182: }
                   1183: 
                   1184: sub retrievestudentphoto {
                   1185:     my ($udom,$unam,$ext,$type) = @_;
                   1186:     my $home=&Apache::lonnet::homeserver($unam,$udom);
                   1187:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
                   1188:     if ($ret eq 'ok') {
                   1189:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   1190:         if ($type eq 'thumbnail') {
                   1191:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   1192:         }
                   1193:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
                   1194:         return $tokenurl;
                   1195:     } else {
                   1196:         if ($type eq 'thumbnail') {
                   1197:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   1198:         } else { 
                   1199:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   1200:         }
1.617     albertel 1201:     }
                   1202: }
                   1203: 
1.263     www      1204: # -------------------------------------------------------------------- New chat
                   1205: 
                   1206: sub chatsend {
1.724     raeburn  1207:     my ($newentry,$anon,$group)=@_;
1.620     albertel 1208:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1209:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1210:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      1211:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 1212: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  1213: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      1214: }
                   1215: 
                   1216: # ------------------------------------------ Find current version of a resource
                   1217: 
                   1218: sub getversion {
                   1219:     my $fname=&clutter(shift);
                   1220:     unless ($fname=~/^\/res\//) { return -1; }
                   1221:     return &currentversion(&filelocation('',$fname));
                   1222: }
                   1223: 
                   1224: sub currentversion {
                   1225:     my $fname=shift;
1.599     albertel 1226:     my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440     www      1227:     if (defined($cached)) { return $result; }
1.292     www      1228:     my $author=$fname;
                   1229:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1230:     my ($udom,$uname)=split(/\//,$author);
                   1231:     my $home=homeserver($uname,$udom);
                   1232:     if ($home eq 'no_host') { 
                   1233:         return -1; 
                   1234:     }
                   1235:     my $answer=reply("currentversion:$fname",$home);
                   1236:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1237: 	return -1;
                   1238:     }
1.599     albertel 1239:     return &do_cache_new('resversion',$fname,$answer,600);
1.263     www      1240: }
                   1241: 
1.1       albertel 1242: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      1243: 
1.1       albertel 1244: sub subscribe {
                   1245:     my $fname=shift;
1.761     raeburn  1246:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 1247:     $fname=~s/[\n\r]//g;
1.1       albertel 1248:     my $author=$fname;
                   1249:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1250:     my ($udom,$uname)=split(/\//,$author);
                   1251:     my $home=homeserver($uname,$udom);
1.335     albertel 1252:     if ($home eq 'no_host') {
                   1253:         return 'not_found';
1.1       albertel 1254:     }
                   1255:     my $answer=reply("sub:$fname",$home);
1.64      www      1256:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1257: 	$answer.=' by '.$home;
                   1258:     }
1.1       albertel 1259:     return $answer;
                   1260: }
                   1261:     
1.8       www      1262: # -------------------------------------------------------------- Replicate file
                   1263: 
                   1264: sub repcopy {
                   1265:     my $filename=shift;
1.23      www      1266:     $filename=~s/\/+/\//g;
1.607     raeburn  1267:     if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
                   1268:     if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 1269:     if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609     banghart 1270: 	$filename=~m -^/*(uploaded|editupload)/-) { 
1.538     albertel 1271: 	return &repcopy_userfile($filename);
                   1272:     }
1.532     albertel 1273:     $filename=~s/[\n\r]//g;
1.8       www      1274:     my $transname="$filename.in.transfer";
1.828     www      1275: # FIXME: this should flock
1.607     raeburn  1276:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      1277:     my $remoteurl=subscribe($filename);
1.64      www      1278:     if ($remoteurl =~ /^con_lost by/) {
                   1279: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  1280:            return 'unavailable';
1.8       www      1281:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 1282: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  1283: 	   return 'not_found';
1.64      www      1284:     } elsif ($remoteurl =~ /^rejected by/) {
                   1285: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  1286:            return 'forbidden';
1.20      www      1287:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  1288:            return 'ok';
1.8       www      1289:     } else {
1.290     www      1290:         my $author=$filename;
                   1291:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1292:         my ($udom,$uname)=split(/\//,$author);
                   1293:         my $home=homeserver($uname,$udom);
                   1294:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      1295:            my @parts=split(/\//,$filename);
                   1296:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   1297:            if ($path ne "$perlvar{'lonDocRoot'}/res") {
                   1298:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  1299: 	       return 'bad_request';
1.8       www      1300:            }
                   1301:            my $count;
                   1302:            for ($count=5;$count<$#parts;$count++) {
                   1303:                $path.="/$parts[$count]";
                   1304:                if ((-e $path)!=1) {
                   1305: 		   mkdir($path,0777);
                   1306:                }
                   1307:            }
                   1308:            my $ua=new LWP::UserAgent;
                   1309:            my $request=new HTTP::Request('GET',"$remoteurl");
                   1310:            my $response=$ua->request($request,$transname);
                   1311:            if ($response->is_error()) {
                   1312: 	       unlink($transname);
                   1313:                my $message=$response->status_line;
1.672     albertel 1314:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      1315:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  1316:                return 'unavailable';
1.8       www      1317:            } else {
1.16      www      1318: 	       if ($remoteurl!~/\.meta$/) {
                   1319:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
                   1320:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
                   1321:                   if ($mresponse->is_error()) {
                   1322: 		      unlink($filename.'.meta');
                   1323:                       &logthis(
1.672     albertel 1324:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      1325:                   }
                   1326: 	       }
1.8       www      1327:                rename($transname,$filename);
1.607     raeburn  1328:                return 'ok';
1.8       www      1329:            }
1.290     www      1330:        }
1.8       www      1331:     }
1.330     www      1332: }
                   1333: 
                   1334: # ------------------------------------------------ Get server side include body
                   1335: sub ssi_body {
1.381     albertel 1336:     my ($filelink,%form)=@_;
1.606     matthew  1337:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   1338:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   1339:     }
1.330     www      1340:     my $output=($filelink=~/^http\:/?&externalssi($filelink):
1.381     albertel 1341:                                      &ssi($filelink,%form));
1.778     albertel 1342:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 1343:     $output=~s/^.*?\<body[^\>]*\>//si;
                   1344:     $output=~s/(.*)\<\/body\s*\>.*?$/$1/si;
1.330     www      1345:     return $output;
1.8       www      1346: }
                   1347: 
1.15      www      1348: # --------------------------------------------------------- Server Side Include
                   1349: 
1.782     albertel 1350: sub absolute_url {
                   1351:     my ($host_name) = @_;
                   1352:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   1353:     if ($host_name eq '') {
                   1354: 	$host_name = $ENV{'SERVER_NAME'};
                   1355:     }
                   1356:     return $protocol.$host_name;
                   1357: }
                   1358: 
1.15      www      1359: sub ssi {
                   1360: 
1.23      www      1361:     my ($fn,%form)=@_;
1.15      www      1362: 
                   1363:     my $ua=new LWP::UserAgent;
1.23      www      1364:     
                   1365:     my $request;
1.711     albertel 1366: 
                   1367:     $form{'no_update_last_known'}=1;
                   1368: 
1.23      www      1369:     if (%form) {
1.782     albertel 1370:       $request=new HTTP::Request('POST',&absolute_url().$fn);
1.201     albertel 1371:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1.23      www      1372:     } else {
1.782     albertel 1373:       $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23      www      1374:     }
                   1375: 
1.15      www      1376:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
                   1377:     my $response=$ua->request($request);
                   1378: 
1.324     www      1379:     return $response->content;
                   1380: }
                   1381: 
                   1382: sub externalssi {
                   1383:     my ($url)=@_;
                   1384:     my $ua=new LWP::UserAgent;
                   1385:     my $request=new HTTP::Request('GET',$url);
                   1386:     my $response=$ua->request($request);
1.15      www      1387:     return $response->content;
                   1388: }
1.254     www      1389: 
1.492     albertel 1390: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   1391: 
                   1392: sub allowuploaded {
                   1393:     my ($srcurl,$url)=@_;
                   1394:     $url=&clutter(&declutter($url));
                   1395:     my $dir=$url;
                   1396:     $dir=~s/\/[^\/]+$//;
                   1397:     my %httpref=();
                   1398:     my $httpurl=&hreflocation('',$url);
                   1399:     $httpref{'httpref.'.$httpurl}=$srcurl;
                   1400:     &Apache::lonnet::appenv(%httpref);
1.254     www      1401: }
1.477     raeburn  1402: 
1.478     albertel 1403: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 1404: # input: action, courseID, current domain, intended
1.637     raeburn  1405: #        path to file, source of file, instruction to parse file for objects,
                   1406: #        ref to hash for embedded objects,
                   1407: #        ref to hash for codebase of java objects.
                   1408: #
1.485     raeburn  1409: # output: url to file (if action was uploaddoc), 
                   1410: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  1411: #
1.478     albertel 1412: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   1413: # course.
1.477     raeburn  1414: #
1.478     albertel 1415: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1416: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   1417: #          course's home server.
1.477     raeburn  1418: #
1.478     albertel 1419: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   1420: #          be copied from $source (current location) to 
                   1421: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1422: #         and will then be copied to
                   1423: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   1424: #         course's home server.
1.485     raeburn  1425: #
1.481     raeburn  1426: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 1427: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  1428: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1429: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   1430: #         in course's home server.
1.637     raeburn  1431: #
1.477     raeburn  1432: 
                   1433: sub process_coursefile {
1.638     albertel 1434:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
1.477     raeburn  1435:     my $fetchresult;
1.638     albertel 1436:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  1437:     if ($action eq 'propagate') {
1.638     albertel 1438:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   1439: 			     $home);
1.481     raeburn  1440:     } else {
1.477     raeburn  1441:         my $fpath = '';
                   1442:         my $fname = $file;
1.478     albertel 1443:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  1444:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  1445:         my $filepath = &build_filepath($fpath);
1.481     raeburn  1446:         if ($action eq 'copy') {
                   1447:             if ($source eq '') {
                   1448:                 $fetchresult = 'no source file';
                   1449:                 return $fetchresult;
                   1450:             } else {
                   1451:                 my $destination = $filepath.'/'.$fname;
                   1452:                 rename($source,$destination);
                   1453:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1454:                                  $home);
1.481     raeburn  1455:             }
                   1456:         } elsif ($action eq 'uploaddoc') {
                   1457:             open(my $fh,'>'.$filepath.'/'.$fname);
1.620     albertel 1458:             print $fh $env{'form.'.$source};
1.481     raeburn  1459:             close($fh);
1.637     raeburn  1460:             if ($parser eq 'parse') {
                   1461:                 my $parse_result = &extract_embedded_items($filepath,$fname,$allfiles,$codebase);
                   1462:                 unless ($parse_result eq 'ok') {
                   1463:                     &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   1464:                 }
                   1465:             }
1.477     raeburn  1466:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1467:                                  $home);
1.481     raeburn  1468:             if ($fetchresult eq 'ok') {
                   1469:                 return '/uploaded/'.$fpath.'/'.$fname;
                   1470:             } else {
                   1471:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 1472:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  1473:                 return '/adm/notfound.html';
                   1474:             }
1.477     raeburn  1475:         }
                   1476:     }
1.485     raeburn  1477:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  1478:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 1479:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  1480:     }
                   1481:     return $fetchresult;
                   1482: }
                   1483: 
1.637     raeburn  1484: sub build_filepath {
                   1485:     my ($fpath) = @_;
                   1486:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   1487:     unless ($fpath eq '') {
                   1488:         my @parts=split('/',$fpath);
                   1489:         foreach my $part (@parts) {
                   1490:             $filepath.= '/'.$part;
                   1491:             if ((-e $filepath)!=1) {
                   1492:                 mkdir($filepath,0777);
                   1493:             }
                   1494:         }
                   1495:     }
                   1496:     return $filepath;
                   1497: }
                   1498: 
                   1499: sub store_edited_file {
1.638     albertel 1500:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  1501:     my $file = $primary_url;
                   1502:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   1503:     my $fpath = '';
                   1504:     my $fname = $file;
                   1505:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   1506:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   1507:     my $filepath = &build_filepath($fpath);
                   1508:     open(my $fh,'>'.$filepath.'/'.$fname);
                   1509:     print $fh $content;
                   1510:     close($fh);
1.638     albertel 1511:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  1512:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1513: 			  $home);
1.637     raeburn  1514:     if ($$fetchresult eq 'ok') {
                   1515:         return '/uploaded/'.$fpath.'/'.$fname;
                   1516:     } else {
1.638     albertel 1517:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   1518: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  1519:         return '/adm/notfound.html';
                   1520:     }
                   1521: }
                   1522: 
1.531     albertel 1523: sub clean_filename {
1.831     albertel 1524:     my ($fname,$args)=@_;
1.315     www      1525: # Replace Windows backslashes by forward slashes
1.257     www      1526:     $fname=~s/\\/\//g;
1.831     albertel 1527:     if (!$args->{'keep_path'}) {
                   1528:         # Get rid of everything but the actual filename
                   1529: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   1530:     }
1.315     www      1531: # Replace spaces by underscores
                   1532:     $fname=~s/\s+/\_/g;
                   1533: # Replace all other weird characters by nothing
1.831     albertel 1534:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 1535: # Replace all .\d. sequences with _\d. so they no longer look like version
                   1536: # numbers
                   1537:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531     albertel 1538:     return $fname;
                   1539: }
                   1540: 
1.608     albertel 1541: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 1542: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.719     banghart 1543: #                    the desired filenam is in $env{"form.$formname.filename"}
1.686     albertel 1544: #        $coursedoc - if true up to the current course
                   1545: #                     if false
                   1546: #        $subdir - directory in userfile to store the file into
1.858     raeburn  1547: #        $parser - instruction to parse file for objects ($parser = parse)    
                   1548: #        $allfiles - reference to hash for embedded objects
                   1549: #        $codebase - reference to hash for codebase of java objects
                   1550: #        $desuname - username for permanent storage of uploaded file
                   1551: #        $dsetudom - domain for permanaent storage of uploaded file
1.860     raeburn  1552: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   1553: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.858     raeburn  1554: # 
1.686     albertel 1555: # output: url of file in userspace, or error: <message> 
                   1556: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 1557: 
                   1558: 
1.531     albertel 1559: sub userfileupload {
1.860     raeburn  1560:     my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
                   1561:         $destudom,$thumbwidth,$thumbheight)=@_;
1.531     albertel 1562:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 1563:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 1564:     $fname=&clean_filename($fname);
1.315     www      1565: # See if there is anything left
1.257     www      1566:     unless ($fname) { return 'error: no uploaded file'; }
1.620     albertel 1567:     chop($env{'form.'.$formname});
1.523     raeburn  1568:     if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
                   1569:         my $now = time;
                   1570:         my $filepath = 'tmp/helprequests/'.$now;
                   1571:         my @parts=split(/\//,$filepath);
                   1572:         my $fullpath = $perlvar{'lonDaemons'};
                   1573:         for (my $i=0;$i<@parts;$i++) {
                   1574:             $fullpath .= '/'.$parts[$i];
                   1575:             if ((-e $fullpath)!=1) {
                   1576:                 mkdir($fullpath,0777);
                   1577:             }
                   1578:         }
                   1579:         open(my $fh,'>'.$fullpath.'/'.$fname);
1.620     albertel 1580:         print $fh $env{'form.'.$formname};
1.523     raeburn  1581:         close($fh);
1.741     raeburn  1582:         return $fullpath.'/'.$fname;
                   1583:     } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) { #files uploaded to create course page are handled differently
                   1584:         my $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   1585:                        '_'.$env{'user.domain'}.'/pending';
                   1586:         my @parts=split(/\//,$filepath);
                   1587:         my $fullpath = $perlvar{'lonDaemons'};
                   1588:         for (my $i=0;$i<@parts;$i++) {
                   1589:             $fullpath .= '/'.$parts[$i];
                   1590:             if ((-e $fullpath)!=1) {
                   1591:                 mkdir($fullpath,0777);
                   1592:             }
                   1593:         }
                   1594:         open(my $fh,'>'.$fullpath.'/'.$fname);
                   1595:         print $fh $env{'form.'.$formname};
                   1596:         close($fh);
                   1597:         return $fullpath.'/'.$fname;
1.523     raeburn  1598:     }
1.719     banghart 1599:     
1.258     www      1600: # Create the directory if not present
1.493     albertel 1601:     $fname="$subdir/$fname";
1.259     www      1602:     if ($coursedoc) {
1.638     albertel 1603: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1604: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  1605:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 1606:             return &finishuserfileupload($docuname,$docudom,
                   1607: 					 $formname,$fname,$parser,$allfiles,
1.860     raeburn  1608: 					 $codebase,$thumbwidth,$thumbheight);
1.481     raeburn  1609:         } else {
1.620     albertel 1610:             $fname=$env{'form.folder'}.'/'.$fname;
1.638     albertel 1611:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   1612: 				       $fname,$formname,$parser,
                   1613: 				       $allfiles,$codebase);
1.481     raeburn  1614:         }
1.719     banghart 1615:     } elsif (defined($destuname)) {
                   1616:         my $docuname=$destuname;
                   1617:         my $docudom=$destudom;
1.860     raeburn  1618: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   1619: 				     $parser,$allfiles,$codebase,
                   1620:                                      $thumbwidth,$thumbheight);
1.719     banghart 1621:         
1.259     www      1622:     } else {
1.638     albertel 1623:         my $docuname=$env{'user.name'};
                   1624:         my $docudom=$env{'user.domain'};
1.714     raeburn  1625:         if (exists($env{'form.group'})) {
                   1626:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1627:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1628:         }
1.860     raeburn  1629: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   1630: 				     $parser,$allfiles,$codebase,
                   1631:                                      $thumbwidth,$thumbheight);
1.259     www      1632:     }
1.271     www      1633: }
                   1634: 
                   1635: sub finishuserfileupload {
1.860     raeburn  1636:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
                   1637:         $thumbwidth,$thumbheight) = @_;
1.477     raeburn  1638:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      1639:     my $filepath=$perlvar{'lonDocRoot'};
1.860     raeburn  1640:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 1641:     $file=$fname;
                   1642:     if ($fname=~m|/|) {
                   1643:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   1644: 	$path.=$fnamepath.'/';
                   1645:     }
1.259     www      1646:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      1647:     my $count;
                   1648:     for ($count=4;$count<=$#parts;$count++) {
                   1649:         $filepath.="/$parts[$count]";
                   1650:         if ((-e $filepath)!=1) {
                   1651: 	    mkdir($filepath,0777);
                   1652:         }
                   1653:     }
                   1654: # Save the file
                   1655:     {
1.701     albertel 1656: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
                   1657: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   1658: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   1659: 	    return '/adm/notfound.html';
                   1660: 	}
                   1661: 	if (!print FH ($env{'form.'.$formname})) {
                   1662: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   1663: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   1664: 	    return '/adm/notfound.html';
                   1665: 	}
1.570     albertel 1666: 	close(FH);
1.258     www      1667:     }
1.637     raeburn  1668:     if ($parser eq 'parse') {
1.638     albertel 1669:         my $parse_result = &extract_embedded_items($filepath,$file,$allfiles,
                   1670: 						   $codebase);
1.637     raeburn  1671:         unless ($parse_result eq 'ok') {
1.638     albertel 1672:             &logthis('Failed to parse '.$filepath.$file.
                   1673: 		     ' for embedded media: '.$parse_result); 
1.637     raeburn  1674:         }
                   1675:     }
1.860     raeburn  1676:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   1677:         my $input = $filepath.'/'.$file;
                   1678:         my $output = $filepath.'/'.'tn-'.$file;
                   1679:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
                   1680:         system("convert -sample $thumbsize $input $output");
                   1681:         if (-e $filepath.'/'.'tn-'.$file) {
                   1682:             $fetchthumb  = 1; 
                   1683:         }
                   1684:     }
1.858     raeburn  1685:  
1.259     www      1686: # Notify homeserver to grep it
                   1687: #
1.638     albertel 1688:     my $docuhome=&homeserver($docuname,$docudom);
1.494     albertel 1689:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      1690:     if ($fetchresult eq 'ok') {
1.860     raeburn  1691:         if ($fetchthumb) {
                   1692:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   1693:             if ($thumbresult ne 'ok') {
                   1694:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   1695:                          $docuhome.': '.$thumbresult);
                   1696:             }
                   1697:         }
1.259     www      1698: #
1.258     www      1699: # Return the URL to it
1.494     albertel 1700:         return '/uploaded/'.$path.$file;
1.263     www      1701:     } else {
1.494     albertel 1702:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   1703: 		 ': '.$fetchresult);
1.263     www      1704:         return '/adm/notfound.html';
1.858     raeburn  1705:     }
1.493     albertel 1706: }
                   1707: 
1.637     raeburn  1708: sub extract_embedded_items {
1.648     raeburn  1709:     my ($filepath,$file,$allfiles,$codebase,$content) = @_;
1.637     raeburn  1710:     my @state = ();
                   1711:     my %javafiles = (
                   1712:                       codebase => '',
                   1713:                       code => '',
                   1714:                       archive => ''
                   1715:                     );
                   1716:     my %mediafiles = (
                   1717:                       src => '',
                   1718:                       movie => '',
                   1719:                      );
1.648     raeburn  1720:     my $p;
                   1721:     if ($content) {
                   1722:         $p = HTML::LCParser->new($content);
                   1723:     } else {
                   1724:         $p = HTML::LCParser->new($filepath.'/'.$file);
                   1725:     }
1.641     albertel 1726:     while (my $t=$p->get_token()) {
1.640     albertel 1727: 	if ($t->[0] eq 'S') {
                   1728: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
                   1729: 	    push (@state, $tagname);
1.648     raeburn  1730:             if (lc($tagname) eq 'allow') {
                   1731:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   1732:             }
1.640     albertel 1733: 	    if (lc($tagname) eq 'img') {
                   1734: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   1735: 	    }
1.645     raeburn  1736:             if (lc($tagname) eq 'script') {
                   1737:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   1738:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   1739:                 } else {
                   1740:                     &add_filetype($allfiles,$attr->{'src'},'src');
                   1741:                 }
                   1742:             }
                   1743:             if (lc($tagname) eq 'link') {
                   1744:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   1745:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   1746:                 }
                   1747:             }
1.640     albertel 1748: 	    if (lc($tagname) eq 'object' ||
                   1749: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   1750: 		foreach my $item (keys(%javafiles)) {
                   1751: 		    $javafiles{$item} = '';
                   1752: 		}
                   1753: 	    }
                   1754: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   1755: 		my $name = lc($attr->{'name'});
                   1756: 		foreach my $item (keys(%javafiles)) {
                   1757: 		    if ($name eq $item) {
                   1758: 			$javafiles{$item} = $attr->{'value'};
                   1759: 			last;
                   1760: 		    }
                   1761: 		}
                   1762: 		foreach my $item (keys(%mediafiles)) {
                   1763: 		    if ($name eq $item) {
                   1764: 			&add_filetype($allfiles, $attr->{'value'}, 'value');
                   1765: 			last;
                   1766: 		    }
                   1767: 		}
                   1768: 	    }
                   1769: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   1770: 		foreach my $item (keys(%javafiles)) {
                   1771: 		    if ($attr->{$item}) {
                   1772: 			$javafiles{$item} = $attr->{$item};
                   1773: 			last;
                   1774: 		    }
                   1775: 		}
                   1776: 		foreach my $item (keys(%mediafiles)) {
                   1777: 		    if ($attr->{$item}) {
                   1778: 			&add_filetype($allfiles,$attr->{$item},$item);
                   1779: 			last;
                   1780: 		    }
                   1781: 		}
                   1782: 	    }
                   1783: 	} elsif ($t->[0] eq 'E') {
                   1784: 	    my ($tagname) = ($t->[1]);
                   1785: 	    if ($javafiles{'codebase'} ne '') {
                   1786: 		$javafiles{'codebase'} .= '/';
                   1787: 	    }  
                   1788: 	    if (lc($tagname) eq 'applet' ||
                   1789: 		lc($tagname) eq 'object' ||
                   1790: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   1791: 		) {
                   1792: 		foreach my $item (keys(%javafiles)) {
                   1793: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   1794: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   1795: 			&add_filetype($allfiles,$file,$item);
                   1796: 		    }
                   1797: 		}
                   1798: 	    } 
                   1799: 	    pop @state;
                   1800: 	}
                   1801:     }
1.637     raeburn  1802:     return 'ok';
                   1803: }
                   1804: 
1.639     albertel 1805: sub add_filetype {
                   1806:     my ($allfiles,$file,$type)=@_;
                   1807:     if (exists($allfiles->{$file})) {
                   1808: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   1809: 	    push(@{$allfiles->{$file}}, &escape($type));
                   1810: 	}
                   1811:     } else {
                   1812: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  1813:     }
                   1814: }
                   1815: 
1.493     albertel 1816: sub removeuploadedurl {
                   1817:     my ($url)=@_;
                   1818:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613     albertel 1819:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 1820: }
                   1821: 
                   1822: sub removeuserfile {
                   1823:     my ($docuname,$docudom,$fname)=@_;
                   1824:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  1825:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
                   1826:     if ($result eq 'ok') {
                   1827:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   1828:             my $metafile = $fname.'.meta';
                   1829:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 1830: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
                   1831:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  1832:             my $sqlresult = 
1.823     albertel 1833:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  1834:                                         'portfolio_metadata',$group,
                   1835:                                         'delete');
1.798     raeburn  1836:         }
                   1837:     }
                   1838:     return $result;
1.257     www      1839: }
1.15      www      1840: 
1.530     albertel 1841: sub mkdiruserfile {
                   1842:     my ($docuname,$docudom,$dir)=@_;
                   1843:     my $home=&homeserver($docuname,$docudom);
                   1844:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   1845: }
                   1846: 
1.531     albertel 1847: sub renameuserfile {
                   1848:     my ($docuname,$docudom,$old,$new)=@_;
                   1849:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  1850:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   1851:                         &escape("$old").':'.&escape("$new"),$home);
                   1852:     if ($result eq 'ok') {
                   1853:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   1854:             my $oldmeta = $old.'.meta';
                   1855:             my $newmeta = $new.'.meta';
                   1856:             my $metaresult = 
                   1857:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 1858: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   1859:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  1860:             my $sqlresult = 
1.823     albertel 1861:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  1862:                                         'portfolio_metadata',$group,
                   1863:                                         'delete');
1.798     raeburn  1864:         }
                   1865:     }
                   1866:     return $result;
1.531     albertel 1867: }
                   1868: 
1.14      www      1869: # ------------------------------------------------------------------------- Log
                   1870: 
                   1871: sub log {
                   1872:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      1873:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      1874: }
                   1875: 
                   1876: # ------------------------------------------------------------------ Course Log
1.352     www      1877: #
                   1878: # This routine flushes several buffers of non-mission-critical nature
                   1879: #
1.157     www      1880: 
                   1881: sub flushcourselogs {
1.352     www      1882:     &logthis('Flushing log buffers');
                   1883: #
                   1884: # course logs
                   1885: # This is a log of all transactions in a course, which can be used
                   1886: # for data mining purposes
                   1887: #
                   1888: # It also collects the courseid database, which lists last transaction
                   1889: # times and course titles for all courseids
                   1890: #
                   1891:     my %courseidbuffer=();
1.800     albertel 1892:     foreach my $crsid (keys %courselogs) {
1.352     www      1893:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      1894: 		          &escape($courselogs{$crsid}),
                   1895: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      1896: 	    delete $courselogs{$crsid};
                   1897:         } else {
                   1898:             &logthis('Failed to flush log buffer for '.$crsid);
                   1899:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 1900:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      1901:                         " exceeded maximum size, deleting.</font>");
                   1902:                delete $courselogs{$crsid};
                   1903:             }
1.352     www      1904:         }
                   1905:         if ($courseidbuffer{$coursehombuf{$crsid}}) {
                   1906:            $courseidbuffer{$coursehombuf{$crsid}}.='&'.
1.516     raeburn  1907: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.741     raeburn  1908:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1.352     www      1909:         } else {
                   1910:            $courseidbuffer{$coursehombuf{$crsid}}=
1.516     raeburn  1911: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.741     raeburn  1912:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1.571     raeburn  1913:         }
1.191     harris41 1914:     }
1.352     www      1915: #
                   1916: # Write course id database (reverse lookup) to homeserver of courses 
                   1917: # Is used in pickcourse
                   1918: #
1.840     albertel 1919:     foreach my $crs_home (keys(%courseidbuffer)) {
1.844     albertel 1920:         &courseidput(&host_domain($crs_home),$courseidbuffer{$crs_home},
1.840     albertel 1921: 		     $crs_home);
1.352     www      1922:     }
                   1923: #
                   1924: # File accesses
                   1925: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   1926: #
1.449     matthew  1927:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  1928:         if ($entry =~ /___count$/) {
                   1929:             my ($dom,$name);
1.807     albertel 1930:             ($dom,$name,undef)=
1.811     albertel 1931: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  1932:             if (! defined($dom) || $dom eq '' || 
                   1933:                 ! defined($name) || $name eq '') {
1.620     albertel 1934:                 my $cid = $env{'request.course.id'};
                   1935:                 $dom  = $env{'request.'.$cid.'.domain'};
                   1936:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  1937:             }
1.450     matthew  1938:             my $value = $accesshash{$entry};
                   1939:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   1940:             my %temphash=($url => $value);
1.449     matthew  1941:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   1942:             if ($result eq 'ok') {
                   1943:                 delete $accesshash{$entry};
                   1944:             } elsif ($result eq 'unknown_cmd') {
                   1945:                 # Target server has old code running on it.
1.450     matthew  1946:                 my %temphash=($entry => $value);
1.449     matthew  1947:                 if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   1948:                     delete $accesshash{$entry};
                   1949:                 }
                   1950:             }
                   1951:         } else {
1.811     albertel 1952:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450     matthew  1953:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  1954:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   1955:                 delete $accesshash{$entry};
                   1956:             }
1.185     www      1957:         }
1.191     harris41 1958:     }
1.352     www      1959: #
                   1960: # Roles
                   1961: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   1962: #
1.800     albertel 1963:     foreach my $entry (keys(%userrolehash)) {
1.351     www      1964:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      1965: 	    split(/\:/,$entry);
                   1966:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      1967:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      1968:                 $rudom,$runame) eq 'ok') {
                   1969: 	    delete $userrolehash{$entry};
                   1970:         }
                   1971:     }
1.662     raeburn  1972: #
                   1973: # Reverse lookup of domain roles (dc, ad, li, sc, au)
                   1974: #
                   1975:     my %domrolebuffer = ();
                   1976:     foreach my $entry (keys %domainrolehash) {
                   1977:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split/:/,$entry;
                   1978:         if ($domrolebuffer{$rudom}) {
                   1979:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   1980:                       '='.&escape($domainrolehash{$entry});
                   1981:         } else {
                   1982:             $domrolebuffer{$rudom}.=&escape($entry).
                   1983:                       '='.&escape($domainrolehash{$entry});
                   1984:         }
                   1985:         delete $domainrolehash{$entry};
                   1986:     }
                   1987:     foreach my $dom (keys(%domrolebuffer)) {
1.841     albertel 1988: 	my %servers = &get_servers($dom,'library');
                   1989: 	foreach my $tryserver (keys(%servers)) {
                   1990: 	    unless (&reply('domroleput:'.$dom.':'.
                   1991: 			   $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   1992: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   1993: 	    }
1.662     raeburn  1994:         }
                   1995:     }
1.186     www      1996:     $dumpcount++;
1.157     www      1997: }
                   1998: 
                   1999: sub courselog {
                   2000:     my $what=shift;
1.158     www      2001:     $what=time.':'.$what;
1.620     albertel 2002:     unless ($env{'request.course.id'}) { return ''; }
                   2003:     $coursedombuf{$env{'request.course.id'}}=
                   2004:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2005:     $coursenumbuf{$env{'request.course.id'}}=
                   2006:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   2007:     $coursehombuf{$env{'request.course.id'}}=
                   2008:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   2009:     $coursedescrbuf{$env{'request.course.id'}}=
                   2010:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   2011:     $courseinstcodebuf{$env{'request.course.id'}}=
                   2012:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   2013:     $courseownerbuf{$env{'request.course.id'}}=
                   2014:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  2015:     $coursetypebuf{$env{'request.course.id'}}=
                   2016:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 2017:     if (defined $courselogs{$env{'request.course.id'}}) {
                   2018: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      2019:     } else {
1.620     albertel 2020: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      2021:     }
1.620     albertel 2022:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      2023: 	&flushcourselogs();
                   2024:     }
1.158     www      2025: }
                   2026: 
                   2027: sub courseacclog {
                   2028:     my $fnsymb=shift;
1.620     albertel 2029:     unless ($env{'request.course.id'}) { return ''; }
                   2030:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657     albertel 2031:     if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187     www      2032:         $what.=':POST';
1.583     matthew  2033:         # FIXME: Probably ought to escape things....
1.800     albertel 2034: 	foreach my $key (keys(%env)) {
                   2035:             if ($key=~/^form\.(.*)/) {
                   2036: 		$what.=':'.$1.'='.$env{$key};
1.158     www      2037:             }
1.191     harris41 2038:         }
1.583     matthew  2039:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   2040:         # FIXME: We should not be depending on a form parameter that someone
                   2041:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 2042:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  2043:             $what.= ':POST';
                   2044:             # FIXME: Probably ought to escape things....
                   2045:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   2046:                                  'crsdiscuss') {
1.620     albertel 2047:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  2048:             }
                   2049:         }
1.158     www      2050:     }
                   2051:     &courselog($what);
1.149     www      2052: }
                   2053: 
1.185     www      2054: sub countacc {
                   2055:     my $url=&declutter(shift);
1.458     matthew  2056:     return if (! defined($url) || $url eq '');
1.620     albertel 2057:     unless ($env{'request.course.id'}) { return ''; }
                   2058:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281     www      2059:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  2060:     $accesshash{$key}++;
1.185     www      2061: }
1.349     www      2062: 
1.361     www      2063: sub linklog {
                   2064:     my ($from,$to)=@_;
                   2065:     $from=&declutter($from);
                   2066:     $to=&declutter($to);
                   2067:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   2068:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   2069: }
                   2070:   
1.349     www      2071: sub userrolelog {
                   2072:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661     raeburn  2073:     if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662     raeburn  2074:         ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661     raeburn  2075:         ($trole=~/^ep/) || ($trole=~/^cr/) ||
                   2076:         ($trole=~/^ta/)) {
1.350     www      2077:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   2078:        $userrolehash
                   2079:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      2080:                     =$tend.':'.$tstart;
1.662     raeburn  2081:     }
                   2082:     if (($trole=~/^dc/) || ($trole=~/^ad/) ||
                   2083:         ($trole=~/^li/) || ($trole=~/^li/) ||
                   2084:         ($trole=~/^au/) || ($trole=~/^dg/) ||
                   2085:         ($trole=~/^sc/)) {
                   2086:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   2087:        $domainrolehash
                   2088:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   2089:                     = $tend.':'.$tstart;
                   2090:     }
1.351     www      2091: }
                   2092: 
                   2093: sub get_course_adv_roles {
                   2094:     my $cid=shift;
1.620     albertel 2095:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      2096:     my %coursehash=&coursedescription($cid);
1.470     www      2097:     my %nothide=();
1.800     albertel 2098:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   2099: 	$nothide{join(':',split(/[\@\:]/,$user))}=1;
1.470     www      2100:     }
1.351     www      2101:     my %returnhash=();
                   2102:     my %dumphash=
                   2103:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   2104:     my $now=time;
1.800     albertel 2105:     foreach my $entry (keys %dumphash) {
                   2106: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      2107:         if (($tstart) && ($tstart<0)) { next; }
                   2108:         if (($tend) && ($tend<$now)) { next; }
                   2109:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 2110:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 2111: 	if ($username eq '' || $domain eq '') { next; }
1.470     www      2112: 	if ((&privileged($username,$domain)) && 
                   2113: 	    (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 2114: 	if ($role eq 'cr') { next; }
1.351     www      2115:         my $key=&plaintext($role);
                   2116:         if ($section) { $key.=' (Sec/Grp '.$section.')'; }
                   2117:         if ($returnhash{$key}) {
                   2118: 	    $returnhash{$key}.=','.$username.':'.$domain;
                   2119:         } else {
                   2120:             $returnhash{$key}=$username.':'.$domain;
                   2121:         }
1.400     www      2122:      }
                   2123:     return %returnhash;
                   2124: }
                   2125: 
                   2126: sub get_my_roles {
1.858     raeburn  2127:     my ($uname,$udom,$context,$types,$roles,$roledoms)=@_;
1.620     albertel 2128:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   2129:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.858     raeburn  2130:     my %dumphash;
                   2131:     if ($context eq 'userroles') { 
                   2132:         %dumphash = &dump('roles',$udom,$uname);
                   2133:     } else {
                   2134:         %dumphash=
1.400     www      2135:             &dump('nohist_userroles',$udom,$uname);
1.858     raeburn  2136:     }
1.400     www      2137:     my %returnhash=();
                   2138:     my $now=time;
1.800     albertel 2139:     foreach my $entry (keys(%dumphash)) {
                   2140: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.400     www      2141:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  2142:         my $status = 'active';
                   2143:         if (($tend) && ($tend<$now)) {
                   2144:             $status = 'previous';
                   2145:         } 
                   2146:         if (($tstart) && ($now<$tstart)) {
                   2147:             $status = 'future';
                   2148:         }
                   2149:         if (ref($types) eq 'ARRAY') {
                   2150:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   2151:                 next;
                   2152:             } 
                   2153:         } else {
                   2154:             if ($status ne 'active') {
                   2155:                 next;
                   2156:             }
                   2157:         }
1.800     albertel 2158:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.832     raeburn  2159:         if (ref($roledoms) eq 'ARRAY') {
                   2160:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   2161:                 next;
                   2162:             }
                   2163:         }
                   2164:         if (ref($roles) eq 'ARRAY') {
                   2165:             if (!grep(/^\Q$role\E$/,@{$roles})) {
                   2166:                 next;
                   2167:             }
                   2168:         } 
1.400     www      2169: 	$returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
1.832     raeburn  2170:     }
1.373     www      2171:     return %returnhash;
1.399     www      2172: }
                   2173: 
                   2174: # ----------------------------------------------------- Frontpage Announcements
                   2175: #
                   2176: #
                   2177: 
                   2178: sub postannounce {
                   2179:     my ($server,$text)=@_;
1.844     albertel 2180:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      2181:     unless ($text=~/\w/) { $text=''; }
                   2182:     return &reply('setannounce:'.&escape($text),$server);
                   2183: }
                   2184: 
                   2185: sub getannounce {
1.448     albertel 2186: 
                   2187:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      2188: 	my $announcement='';
1.800     albertel 2189: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 2190: 	close($fh);
1.399     www      2191: 	if ($announcement=~/\w/) { 
                   2192: 	    return 
                   2193:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 2194:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      2195: 	} else {
                   2196: 	    return '';
                   2197: 	}
                   2198:     } else {
                   2199: 	return '';
                   2200:     }
1.351     www      2201: }
1.353     www      2202: 
                   2203: # ---------------------------------------------------------- Course ID routines
                   2204: # Deal with domain's nohist_courseid.db files
                   2205: #
                   2206: 
                   2207: sub courseidput {
                   2208:     my ($domain,$what,$coursehome)=@_;
                   2209:     return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   2210: }
                   2211: 
                   2212: sub courseiddump {
1.791     raeburn  2213:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,$coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok)=@_;
1.353     www      2214:     my %returnhash=();
1.355     www      2215:     unless ($domfilter) { $domfilter=''; }
1.845     albertel 2216:     my %libserv = &all_library();
                   2217:     foreach my $tryserver (keys(%libserv)) {
                   2218:         if ( (  $hostidflag == 1 
                   2219: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   2220: 	     || (!defined($hostidflag)) ) {
                   2221: 
                   2222: 	    if ($domfilter eq ''
                   2223: 		|| (&host_domain($tryserver) eq $domfilter)) {
1.800     albertel 2224: 	        foreach my $line (
1.844     albertel 2225:                  split(/\&/,&reply('courseiddump:'.&host_domain($tryserver).':'.
1.571     raeburn  2226: 			       $sincefilter.':'.&escape($descfilter).':'.
1.791     raeburn  2227:                                &escape($instcodefilter).':'.&escape($ownerfilter).':'.&escape($coursefilter).':'.&escape($typefilter).':'.&escape($regexp_ok),
1.354     www      2228:                                $tryserver))) {
1.800     albertel 2229: 		    my ($key,$value)=split(/\=/,$line,2);
1.506     raeburn  2230:                     if (($key) && ($value)) {
1.516     raeburn  2231: 		        $returnhash{&unescape($key)}=$value;
1.506     raeburn  2232:                     }
1.353     www      2233:                 }
                   2234:             }
                   2235:         }
                   2236:     }
                   2237:     return %returnhash;
                   2238: }
                   2239: 
1.658     raeburn  2240: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  2241: 
                   2242: sub dcmailput {
1.685     raeburn  2243:     my ($domain,$msgid,$message,$server)=@_;
1.662     raeburn  2244:     my $status = &Apache::lonnet::critical(
1.740     www      2245:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   2246:        &escape($message),$server);
1.662     raeburn  2247:     return $status;
                   2248: }
                   2249: 
1.658     raeburn  2250: sub dcmaildump {
                   2251:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  2252:     my %returnhash=();
1.846     albertel 2253: 
                   2254:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  2255:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   2256:                                                          &escape($enddate).':';
                   2257: 	my @esc_senders=map { &escape($_)} @$senders;
                   2258: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 2259: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 2260:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  2261:             if (($key) && ($value)) {
                   2262:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  2263:             }
                   2264:         }
                   2265:     }
                   2266:     return %returnhash;
                   2267: }
1.662     raeburn  2268: # ---------------------------------------------------------- Domain roles
                   2269: 
                   2270: sub get_domain_roles {
                   2271:     my ($dom,$roles,$startdate,$enddate)=@_;
                   2272:     if (undef($startdate) || $startdate eq '') {
                   2273:         $startdate = '.';
                   2274:     }
                   2275:     if (undef($enddate) || $enddate eq '') {
                   2276:         $enddate = '.';
                   2277:     }
                   2278:     my $rolelist = join(':',@{$roles});
                   2279:     my %personnel = ();
1.841     albertel 2280: 
                   2281:     my %servers = &get_servers($dom,'library');
                   2282:     foreach my $tryserver (keys(%servers)) {
                   2283: 	%{$personnel{$tryserver}}=();
                   2284: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   2285: 					    &escape($startdate).':'.
                   2286: 					    &escape($enddate).':'.
                   2287: 					    &escape($rolelist), $tryserver))) {
                   2288: 	    my ($key,$value) = split(/\=/,$line,2);
                   2289: 	    if (($key) && ($value)) {
                   2290: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   2291: 	    }
                   2292: 	}
1.662     raeburn  2293:     }
                   2294:     return %personnel;
                   2295: }
1.658     raeburn  2296: 
1.149     www      2297: # ----------------------------------------------------------- Check out an item
                   2298: 
1.504     albertel 2299: sub get_first_access {
                   2300:     my ($type,$argsymb)=@_;
1.790     albertel 2301:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 2302:     if ($argsymb) { $symb=$argsymb; }
                   2303:     my ($map,$id,$res)=&decode_symb($symb);
1.588     albertel 2304:     if ($type eq 'map') {
                   2305: 	$res=&symbread($map);
                   2306:     } else {
                   2307: 	$res=$symb;
                   2308:     }
                   2309:     my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
                   2310:     return $times{"$courseid\0$res"};
1.504     albertel 2311: }
                   2312: 
                   2313: sub set_first_access {
                   2314:     my ($type)=@_;
1.790     albertel 2315:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 2316:     my ($map,$id,$res)=&decode_symb($symb);
1.588     albertel 2317:     if ($type eq 'map') {
                   2318: 	$res=&symbread($map);
                   2319:     } else {
                   2320: 	$res=$symb;
                   2321:     }
                   2322:     my $firstaccess=&get_first_access($type,$symb);
1.505     albertel 2323:     if (!$firstaccess) {
1.588     albertel 2324: 	return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505     albertel 2325:     }
                   2326:     return 'already_set';
1.504     albertel 2327: }
                   2328: 
1.149     www      2329: sub checkout {
                   2330:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
                   2331:     my $now=time;
                   2332:     my $lonhost=$perlvar{'lonHostID'};
                   2333:     my $infostr=&escape(
1.234     www      2334:                  'CHECKOUTTOKEN&'.
1.149     www      2335:                  $tuname.'&'.
                   2336:                  $tudom.'&'.
                   2337:                  $tcrsid.'&'.
                   2338:                  $symb.'&'.
                   2339: 		 $now.'&'.$ENV{'REMOTE_ADDR'});
                   2340:     my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151     www      2341:     if ($token=~/^error\:/) { 
1.672     albertel 2342:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2343:                 "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2344:                  "</font>");
                   2345:         return ''; 
                   2346:     }
                   2347: 
1.149     www      2348:     $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
                   2349:     $token=~tr/a-z/A-Z/;
                   2350: 
1.153     www      2351:     my %infohash=('resource.0.outtoken' => $token,
                   2352:                   'resource.0.checkouttime' => $now,
                   2353:                   'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149     www      2354: 
                   2355:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   2356:        return '';
1.151     www      2357:     } else {
1.672     albertel 2358:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2359:                 "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2360:                  "</font>");
1.149     www      2361:     }    
                   2362: 
                   2363:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   2364:                          &escape('Checkout '.$infostr.' - '.
                   2365:                                                  $token)) ne 'ok') {
                   2366: 	return '';
1.151     www      2367:     } else {
1.672     albertel 2368:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2369:                 "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2370:                  "</font>");
1.149     www      2371:     }
1.151     www      2372:     return $token;
1.149     www      2373: }
                   2374: 
                   2375: # ------------------------------------------------------------ Check in an item
                   2376: 
                   2377: sub checkin {
                   2378:     my $token=shift;
1.150     www      2379:     my $now=time;
                   2380:     my ($ta,$tb,$lonhost)=split(/\*/,$token);
                   2381:     $lonhost=~tr/A-Z/a-z/;
1.838     albertel 2382:     my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
1.150     www      2383:     $dtoken=~s/\W/\_/g;
1.234     www      2384:     my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150     www      2385:                  split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
                   2386: 
1.154     www      2387:     unless (($tuname) && ($tudom)) {
                   2388:         &logthis('Check in '.$token.' ('.$dtoken.') failed');
                   2389:         return '';
                   2390:     }
                   2391:     
                   2392:     unless (&allowed('mgr',$tcrsid)) {
                   2393:         &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1.620     albertel 2394:                  $env{'user.name'}.' - '.$env{'user.domain'});
1.154     www      2395:         return '';
                   2396:     }
                   2397: 
1.153     www      2398:     my %infohash=('resource.0.intoken' => $token,
                   2399:                   'resource.0.checkintime' => $now,
                   2400:                   'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150     www      2401: 
                   2402:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   2403:        return '';
                   2404:     }    
                   2405: 
                   2406:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   2407:                          &escape('Checkin - '.$token)) ne 'ok') {
                   2408: 	return '';
                   2409:     }
                   2410: 
                   2411:     return ($symb,$tuname,$tudom,$tcrsid);    
1.110     www      2412: }
                   2413: 
                   2414: # --------------------------------------------- Set Expire Date for Spreadsheet
                   2415: 
                   2416: sub expirespread {
                   2417:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 2418:     my $cid=$env{'request.course.id'}; 
1.110     www      2419:     if ($cid) {
                   2420:        my $now=time;
                   2421:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 2422:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   2423:                             $env{'course.'.$cid.'.num'}.
1.110     www      2424: 	        	    ':nohist_expirationdates:'.
                   2425:                             &escape($key).'='.$now,
1.620     albertel 2426:                             $env{'course.'.$cid.'.home'})
1.110     www      2427:     }
                   2428:     return 'ok';
1.14      www      2429: }
                   2430: 
1.109     www      2431: # ----------------------------------------------------- Devalidate Spreadsheets
                   2432: 
                   2433: sub devalidate {
1.325     www      2434:     my ($symb,$uname,$udom)=@_;
1.620     albertel 2435:     my $cid=$env{'request.course.id'}; 
1.109     www      2436:     if ($cid) {
1.391     matthew  2437:         # delete the stored spreadsheets for
                   2438:         # - the student level sheet of this user in course's homespace
                   2439:         # - the assessment level sheet for this resource 
                   2440:         #   for this user in user's homespace
1.553     albertel 2441: 	# - current conditional state info
1.325     www      2442: 	my $key=$uname.':'.$udom.':';
1.109     www      2443:         my $status=
1.299     matthew  2444: 	    &del('nohist_calculatedsheets',
1.391     matthew  2445: 		 [$key.'studentcalc:'],
1.620     albertel 2446: 		 $env{'course.'.$cid.'.domain'},
                   2447: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 2448: 		.' '.
                   2449: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  2450: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      2451:         unless ($status eq 'ok ok') {
                   2452:            &logthis('Could not devalidate spreadsheet '.
1.325     www      2453:                     $uname.' at '.$udom.' for '.
1.109     www      2454: 		    $symb.': '.$status);
1.133     albertel 2455:         }
1.553     albertel 2456: 	&delenv('user.state.'.$cid);
1.109     www      2457:     }
                   2458: }
                   2459: 
1.265     albertel 2460: sub get_scalar {
                   2461:     my ($string,$end) = @_;
                   2462:     my $value;
                   2463:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   2464: 	$value = $1;
                   2465:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   2466: 	$value = $1;
                   2467:     }
                   2468:     return &unescape($value);
                   2469: }
                   2470: 
                   2471: sub array2str {
                   2472:   my (@array) = @_;
                   2473:   my $result=&arrayref2str(\@array);
                   2474:   $result=~s/^__ARRAY_REF__//;
                   2475:   $result=~s/__END_ARRAY_REF__$//;
                   2476:   return $result;
                   2477: }
                   2478: 
1.204     albertel 2479: sub arrayref2str {
                   2480:   my ($arrayref) = @_;
1.265     albertel 2481:   my $result='__ARRAY_REF__';
1.204     albertel 2482:   foreach my $elem (@$arrayref) {
1.265     albertel 2483:     if(ref($elem) eq 'ARRAY') {
                   2484:       $result.=&arrayref2str($elem).'&';
                   2485:     } elsif(ref($elem) eq 'HASH') {
                   2486:       $result.=&hashref2str($elem).'&';
                   2487:     } elsif(ref($elem)) {
                   2488:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 2489:     } else {
                   2490:       $result.=&escape($elem).'&';
                   2491:     }
                   2492:   }
                   2493:   $result=~s/\&$//;
1.265     albertel 2494:   $result .= '__END_ARRAY_REF__';
1.204     albertel 2495:   return $result;
                   2496: }
                   2497: 
1.168     albertel 2498: sub hash2str {
1.204     albertel 2499:   my (%hash) = @_;
                   2500:   my $result=&hashref2str(\%hash);
1.265     albertel 2501:   $result=~s/^__HASH_REF__//;
                   2502:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 2503:   return $result;
                   2504: }
                   2505: 
                   2506: sub hashref2str {
                   2507:   my ($hashref)=@_;
1.265     albertel 2508:   my $result='__HASH_REF__';
1.800     albertel 2509:   foreach my $key (sort(keys(%$hashref))) {
                   2510:     if (ref($key) eq 'ARRAY') {
                   2511:       $result.=&arrayref2str($key).'=';
                   2512:     } elsif (ref($key) eq 'HASH') {
                   2513:       $result.=&hashref2str($key).'=';
                   2514:     } elsif (ref($key)) {
1.265     albertel 2515:       $result.='=';
1.800     albertel 2516:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 2517:     } else {
1.800     albertel 2518: 	if ($key) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 2519:     }
                   2520: 
1.800     albertel 2521:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   2522:       $result.=&arrayref2str($hashref->{$key}).'&';
                   2523:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   2524:       $result.=&hashref2str($hashref->{$key}).'&';
                   2525:     } elsif(ref($hashref->{$key})) {
1.265     albertel 2526:        $result.='&';
1.800     albertel 2527:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 2528:     } else {
1.800     albertel 2529:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 2530:     }
                   2531:   }
1.168     albertel 2532:   $result=~s/\&$//;
1.265     albertel 2533:   $result .= '__END_HASH_REF__';
1.168     albertel 2534:   return $result;
                   2535: }
                   2536: 
                   2537: sub str2hash {
1.265     albertel 2538:     my ($string)=@_;
                   2539:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   2540:     return %$hash;
                   2541: }
                   2542: 
                   2543: sub str2hashref {
1.168     albertel 2544:   my ($string) = @_;
1.265     albertel 2545: 
                   2546:   my %hash;
                   2547: 
                   2548:   if($string !~ /^__HASH_REF__/) {
                   2549:       if (! ($string eq '' || !defined($string))) {
                   2550: 	  $hash{'error'}='Not hash reference';
                   2551:       }
                   2552:       return (\%hash, $string);
                   2553:   }
                   2554: 
                   2555:   $string =~ s/^__HASH_REF__//;
                   2556: 
                   2557:   while($string !~ /^__END_HASH_REF__/) {
                   2558:       #key
                   2559:       my $key='';
                   2560:       if($string =~ /^__HASH_REF__/) {
                   2561:           ($key, $string)=&str2hashref($string);
                   2562:           if(defined($key->{'error'})) {
                   2563:               $hash{'error'}='Bad data';
                   2564:               return (\%hash, $string);
                   2565:           }
                   2566:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2567:           ($key, $string)=&str2arrayref($string);
                   2568:           if($key->[0] eq 'Array reference error') {
                   2569:               $hash{'error'}='Bad data';
                   2570:               return (\%hash, $string);
                   2571:           }
                   2572:       } else {
                   2573:           $string =~ s/^(.*?)=//;
1.267     albertel 2574: 	  $key=&unescape($1);
1.265     albertel 2575:       }
                   2576:       $string =~ s/^=//;
                   2577: 
                   2578:       #value
                   2579:       my $value='';
                   2580:       if($string =~ /^__HASH_REF__/) {
                   2581:           ($value, $string)=&str2hashref($string);
                   2582:           if(defined($value->{'error'})) {
                   2583:               $hash{'error'}='Bad data';
                   2584:               return (\%hash, $string);
                   2585:           }
                   2586:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2587:           ($value, $string)=&str2arrayref($string);
                   2588:           if($value->[0] eq 'Array reference error') {
                   2589:               $hash{'error'}='Bad data';
                   2590:               return (\%hash, $string);
                   2591:           }
                   2592:       } else {
                   2593: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   2594:       }
                   2595:       $string =~ s/^&//;
                   2596: 
                   2597:       $hash{$key}=$value;
1.204     albertel 2598:   }
1.265     albertel 2599: 
                   2600:   $string =~ s/^__END_HASH_REF__//;
                   2601: 
                   2602:   return (\%hash, $string);
1.204     albertel 2603: }
                   2604: 
                   2605: sub str2array {
1.265     albertel 2606:     my ($string)=@_;
                   2607:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   2608:     return @$array;
                   2609: }
                   2610: 
                   2611: sub str2arrayref {
1.204     albertel 2612:   my ($string) = @_;
1.265     albertel 2613:   my @array;
                   2614: 
                   2615:   if($string !~ /^__ARRAY_REF__/) {
                   2616:       if (! ($string eq '' || !defined($string))) {
                   2617: 	  $array[0]='Array reference error';
                   2618:       }
                   2619:       return (\@array, $string);
                   2620:   }
                   2621: 
                   2622:   $string =~ s/^__ARRAY_REF__//;
                   2623: 
                   2624:   while($string !~ /^__END_ARRAY_REF__/) {
                   2625:       my $value='';
                   2626:       if($string =~ /^__HASH_REF__/) {
                   2627:           ($value, $string)=&str2hashref($string);
                   2628:           if(defined($value->{'error'})) {
                   2629:               $array[0] ='Array reference error';
                   2630:               return (\@array, $string);
                   2631:           }
                   2632:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2633:           ($value, $string)=&str2arrayref($string);
                   2634:           if($value->[0] eq 'Array reference error') {
                   2635:               $array[0] ='Array reference error';
                   2636:               return (\@array, $string);
                   2637:           }
                   2638:       } else {
                   2639: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   2640:       }
                   2641:       $string =~ s/^&//;
                   2642: 
                   2643:       push(@array, $value);
1.191     harris41 2644:   }
1.265     albertel 2645: 
                   2646:   $string =~ s/^__END_ARRAY_REF__//;
                   2647: 
                   2648:   return (\@array, $string);
1.168     albertel 2649: }
                   2650: 
1.167     albertel 2651: # -------------------------------------------------------------------Temp Store
                   2652: 
1.168     albertel 2653: sub tmpreset {
                   2654:   my ($symb,$namespace,$domain,$stuname) = @_;
                   2655:   if (!$symb) {
                   2656:     $symb=&symbread();
1.620     albertel 2657:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2658:   }
                   2659:   $symb=escape($symb);
                   2660: 
1.620     albertel 2661:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 2662:   $namespace=~s/\//\_/g;
                   2663:   $namespace=~s/\W//g;
                   2664: 
1.620     albertel 2665:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2666:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2667:   if ($domain eq 'public' && $stuname eq 'public') {
                   2668:       $stuname=$ENV{'REMOTE_ADDR'};
                   2669:   }
1.168     albertel 2670:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2671:   my %hash;
                   2672:   if (tie(%hash,'GDBM_File',
                   2673: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2674: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 2675:     foreach my $key (keys %hash) {
1.180     albertel 2676:       if ($key=~ /:$symb/) {
1.168     albertel 2677: 	delete($hash{$key});
                   2678:       }
                   2679:     }
                   2680:   }
                   2681: }
                   2682: 
1.167     albertel 2683: sub tmpstore {
1.168     albertel 2684:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2685: 
                   2686:   if (!$symb) {
                   2687:     $symb=&symbread();
1.620     albertel 2688:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2689:   }
                   2690:   $symb=escape($symb);
                   2691: 
                   2692:   if (!$namespace) {
                   2693:     # I don't think we would ever want to store this for a course.
                   2694:     # it seems this will only be used if we don't have a course.
1.620     albertel 2695:     #$namespace=$env{'request.course.id'};
1.168     albertel 2696:     #if (!$namespace) {
1.620     albertel 2697:       $namespace=$env{'request.state'};
1.168     albertel 2698:     #}
                   2699:   }
                   2700:   $namespace=~s/\//\_/g;
                   2701:   $namespace=~s/\W//g;
1.620     albertel 2702:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2703:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2704:   if ($domain eq 'public' && $stuname eq 'public') {
                   2705:       $stuname=$ENV{'REMOTE_ADDR'};
                   2706:   }
1.168     albertel 2707:   my $now=time;
                   2708:   my %hash;
                   2709:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2710:   if (tie(%hash,'GDBM_File',
                   2711: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2712: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 2713:     $hash{"version:$symb"}++;
                   2714:     my $version=$hash{"version:$symb"};
                   2715:     my $allkeys=''; 
                   2716:     foreach my $key (keys(%$storehash)) {
                   2717:       $allkeys.=$key.':';
1.591     albertel 2718:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 2719:     }
                   2720:     $hash{"$version:$symb:timestamp"}=$now;
                   2721:     $allkeys.='timestamp';
                   2722:     $hash{"$version:keys:$symb"}=$allkeys;
                   2723:     if (untie(%hash)) {
                   2724:       return 'ok';
                   2725:     } else {
                   2726:       return "error:$!";
                   2727:     }
                   2728:   } else {
                   2729:     return "error:$!";
                   2730:   }
                   2731: }
1.167     albertel 2732: 
1.168     albertel 2733: # -----------------------------------------------------------------Temp Restore
1.167     albertel 2734: 
1.168     albertel 2735: sub tmprestore {
                   2736:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 2737: 
1.168     albertel 2738:   if (!$symb) {
                   2739:     $symb=&symbread();
1.620     albertel 2740:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2741:   }
                   2742:   $symb=escape($symb);
                   2743: 
1.620     albertel 2744:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 2745: 
1.620     albertel 2746:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2747:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2748:   if ($domain eq 'public' && $stuname eq 'public') {
                   2749:       $stuname=$ENV{'REMOTE_ADDR'};
                   2750:   }
1.168     albertel 2751:   my %returnhash;
                   2752:   $namespace=~s/\//\_/g;
                   2753:   $namespace=~s/\W//g;
                   2754:   my %hash;
                   2755:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2756:   if (tie(%hash,'GDBM_File',
                   2757: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2758: 	  &GDBM_READER(),0640)) {
1.168     albertel 2759:     my $version=$hash{"version:$symb"};
                   2760:     $returnhash{'version'}=$version;
                   2761:     my $scope;
                   2762:     for ($scope=1;$scope<=$version;$scope++) {
                   2763:       my $vkeys=$hash{"$scope:keys:$symb"};
                   2764:       my @keys=split(/:/,$vkeys);
                   2765:       my $key;
                   2766:       $returnhash{"$scope:keys"}=$vkeys;
                   2767:       foreach $key (@keys) {
1.591     albertel 2768: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   2769: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 2770:       }
                   2771:     }
1.168     albertel 2772:     if (!(untie(%hash))) {
                   2773:       return "error:$!";
                   2774:     }
                   2775:   } else {
                   2776:     return "error:$!";
                   2777:   }
                   2778:   return %returnhash;
1.167     albertel 2779: }
                   2780: 
1.9       www      2781: # ----------------------------------------------------------------------- Store
                   2782: 
                   2783: sub store {
1.124     www      2784:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2785:     my $home='';
                   2786: 
1.168     albertel 2787:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2788: 
1.213     www      2789:     $symb=&symbclean($symb);
1.122     albertel 2790:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      2791: 
1.620     albertel 2792:     if (!$domain) { $domain=$env{'user.domain'}; }
                   2793:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      2794: 
                   2795:     &devalidate($symb,$stuname,$domain);
1.109     www      2796: 
                   2797:     $symb=escape($symb);
1.187     www      2798:     if (!$namespace) { 
1.620     albertel 2799:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      2800:           return ''; 
                   2801:        } 
                   2802:     }
1.620     albertel 2803:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      2804: 
                   2805:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   2806:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   2807: 
1.12      www      2808:     my $namevalue='';
1.800     albertel 2809:     foreach my $key (keys(%$storehash)) {
                   2810:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 2811:     }
1.12      www      2812:     $namevalue=~s/\&$//;
1.187     www      2813:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124     www      2814:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9       www      2815: }
                   2816: 
1.47      www      2817: # -------------------------------------------------------------- Critical Store
                   2818: 
                   2819: sub cstore {
1.124     www      2820:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2821:     my $home='';
                   2822: 
1.168     albertel 2823:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2824: 
1.213     www      2825:     $symb=&symbclean($symb);
1.122     albertel 2826:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      2827: 
1.620     albertel 2828:     if (!$domain) { $domain=$env{'user.domain'}; }
                   2829:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      2830: 
                   2831:     &devalidate($symb,$stuname,$domain);
1.109     www      2832: 
                   2833:     $symb=escape($symb);
1.187     www      2834:     if (!$namespace) { 
1.620     albertel 2835:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      2836:           return ''; 
                   2837:        } 
                   2838:     }
1.620     albertel 2839:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      2840: 
                   2841:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   2842:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 2843: 
1.47      www      2844:     my $namevalue='';
1.800     albertel 2845:     foreach my $key (keys(%$storehash)) {
                   2846:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 2847:     }
1.47      www      2848:     $namevalue=~s/\&$//;
1.187     www      2849:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      2850:     return critical
                   2851:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47      www      2852: }
                   2853: 
1.9       www      2854: # --------------------------------------------------------------------- Restore
                   2855: 
                   2856: sub restore {
1.124     www      2857:     my ($symb,$namespace,$domain,$stuname) = @_;
                   2858:     my $home='';
                   2859: 
1.168     albertel 2860:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2861: 
1.122     albertel 2862:     if (!$symb) {
                   2863:       unless ($symb=escape(&symbread())) { return ''; }
                   2864:     } else {
1.213     www      2865:       $symb=&escape(&symbclean($symb));
1.122     albertel 2866:     }
1.188     www      2867:     if (!$namespace) { 
1.620     albertel 2868:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      2869:           return ''; 
                   2870:        } 
                   2871:     }
1.620     albertel 2872:     if (!$domain) { $domain=$env{'user.domain'}; }
                   2873:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   2874:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 2875:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   2876: 
1.12      www      2877:     my %returnhash=();
1.800     albertel 2878:     foreach my $line (split(/\&/,$answer)) {
                   2879: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 2880:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 2881:     }
1.75      www      2882:     my $version;
                   2883:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 2884:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   2885:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 2886:        }
1.75      www      2887:     }
1.13      www      2888:     return %returnhash;
1.34      www      2889: }
                   2890: 
                   2891: # ---------------------------------------------------------- Course Description
                   2892: 
                   2893: sub coursedescription {
1.731     albertel 2894:     my ($courseid,$args)=@_;
1.34      www      2895:     $courseid=~s/^\///;
1.49      www      2896:     $courseid=~s/\_/\//g;
1.34      www      2897:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 2898:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 2899:     my $normalid=$cdomain.'_'.$cnum;
                   2900:     # need to always cache even if we get errors otherwise we keep 
                   2901:     # trying and trying and trying to get the course description.
                   2902:     my %envhash=();
                   2903:     my %returnhash=();
1.731     albertel 2904:     
                   2905:     my $expiretime=600;
                   2906:     if ($env{'request.course.id'} eq $normalid) {
                   2907: 	$expiretime=120;
                   2908:     }
                   2909: 
                   2910:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   2911:     if (!$args->{'freshen_cache'}
                   2912: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   2913: 	foreach my $key (keys(%env)) {
                   2914: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   2915: 	    my ($setting) = $1;
                   2916: 	    $returnhash{$setting} = $env{$key};
                   2917: 	}
                   2918: 	return %returnhash;
                   2919:     }
                   2920: 
                   2921:     # get the data agin
                   2922:     if (!$args->{'one_time'}) {
                   2923: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   2924:     }
1.811     albertel 2925: 
1.34      www      2926:     if ($chome ne 'no_host') {
1.302     albertel 2927:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 2928:        if (!exists($returnhash{'con_lost'})) {
                   2929:            $returnhash{'home'}= $chome;
                   2930: 	   $returnhash{'domain'} = $cdomain;
                   2931: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  2932:            if (!defined($returnhash{'type'})) {
                   2933:                $returnhash{'type'} = 'Course';
                   2934:            }
1.130     albertel 2935:            while (my ($name,$value) = each %returnhash) {
1.53      www      2936:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 2937:            }
1.270     www      2938:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.34      www      2939:            $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620     albertel 2940: 	       $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60      www      2941:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   2942:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   2943:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      2944:        }
                   2945:     }
1.731     albertel 2946:     if (!$args->{'one_time'}) {
                   2947: 	&appenv(%envhash);
                   2948:     }
1.302     albertel 2949:     return %returnhash;
1.461     www      2950: }
                   2951: 
                   2952: # -------------------------------------------------See if a user is privileged
                   2953: 
                   2954: sub privileged {
                   2955:     my ($username,$domain)=@_;
                   2956:     my $rolesdump=&reply("dump:$domain:$username:roles",
                   2957: 			&homeserver($username,$domain));
                   2958:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
                   2959:     my $now=time;
                   2960:     if ($rolesdump ne '') {
1.800     albertel 2961:         foreach my $entry (split(/&/,$rolesdump)) {
                   2962: 	    if ($entry!~/^rolesdef_/) {
                   2963: 		my ($area,$role)=split(/=/,$entry);
1.461     www      2964: 		$area=~s/\_\w\w$//;
                   2965: 		my ($trole,$tend,$tstart)=split(/_/,$role);
                   2966: 		if (($trole eq 'dc') || ($trole eq 'su')) {
                   2967: 		    my $active=1;
                   2968: 		    if ($tend) {
                   2969: 			if ($tend<$now) { $active=0; }
                   2970: 		    }
                   2971: 		    if ($tstart) {
                   2972: 			if ($tstart>$now) { $active=0; }
                   2973: 		    }
                   2974: 		    if ($active) { return 1; }
                   2975: 		}
                   2976: 	    }
                   2977: 	}
                   2978:     }
                   2979:     return 0;
1.9       www      2980: }
1.1       albertel 2981: 
1.103     harris41 2982: # -------------------------------------------------------- Get user privileges
1.11      www      2983: 
                   2984: sub rolesinit {
                   2985:     my ($domain,$username,$authhost)=@_;
                   2986:     my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.12      www      2987:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1.11      www      2988:     my %allroles=();
1.678     raeburn  2989:     my %allgroups=();   
1.11      www      2990:     my $now=time;
1.743     albertel 2991:     my %userroles = ('user.login.time' => $now);
1.678     raeburn  2992:     my $group_privs;
1.11      www      2993: 
                   2994:     if ($rolesdump ne '') {
1.800     albertel 2995:         foreach my $entry (split(/&/,$rolesdump)) {
                   2996: 	  if ($entry!~/^rolesdef_/) {
                   2997:             my ($area,$role)=split(/=/,$entry);
1.587     albertel 2998: 	    $area=~s/\_\w\w$//;
1.678     raeburn  2999:             my ($trole,$tend,$tstart,$group_privs);
1.587     albertel 3000: 	    if ($role=~/^cr/) { 
1.807     albertel 3001: 		if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   3002: 		    ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655     albertel 3003: 		    ($tend,$tstart)=split('_',$trest);
                   3004: 		} else {
                   3005: 		    $trole=$role;
                   3006: 		}
1.678     raeburn  3007:             } elsif ($role =~ m|^gr/|) {
                   3008:                 ($trole,$tend,$tstart) = split(/_/,$role);
                   3009:                 ($trole,$group_privs) = split(/\//,$trole);
                   3010:                 $group_privs = &unescape($group_privs);
1.587     albertel 3011: 	    } else {
                   3012: 		($trole,$tend,$tstart)=split(/_/,$role);
                   3013: 	    }
1.743     albertel 3014: 	    my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   3015: 					 $username);
                   3016: 	    @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567     raeburn  3017:             if (($tend!=0) && ($tend<$now)) { $trole=''; }
                   3018:             if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11      www      3019:             if (($area ne '') && ($trole ne '')) {
1.347     albertel 3020: 		my $spec=$trole.'.'.$area;
                   3021: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   3022: 		if ($trole =~ /^cr\//) {
1.567     raeburn  3023:                     &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678     raeburn  3024:                 } elsif ($trole eq 'gr') {
                   3025:                     &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347     albertel 3026: 		} else {
1.567     raeburn  3027:                     &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347     albertel 3028: 		}
1.12      www      3029:             }
1.662     raeburn  3030:           }
1.191     harris41 3031:         }
1.743     albertel 3032:         my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
                   3033:         $userroles{'user.adv'}    = $adv;
                   3034: 	$userroles{'user.author'} = $author;
1.620     albertel 3035:         $env{'user.adv'}=$adv;
1.11      www      3036:     }
1.743     albertel 3037:     return \%userroles;  
1.11      www      3038: }
                   3039: 
1.567     raeburn  3040: sub set_arearole {
                   3041:     my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
                   3042: # log the associated role with the area
                   3043:     &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743     albertel 3044:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  3045: }
                   3046: 
                   3047: sub custom_roleprivs {
                   3048:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   3049:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
                   3050:     my $homsvr=homeserver($rauthor,$rdomain);
1.838     albertel 3051:     if (&hostname($homsvr) ne '') {
1.567     raeburn  3052:         my ($rdummy,$roledef)=
                   3053:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   3054:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   3055:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   3056:             if (defined($syspriv)) {
                   3057:                 $$allroles{'cm./'}.=':'.$syspriv;
                   3058:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   3059:             }
                   3060:             if ($tdomain ne '') {
                   3061:                 if (defined($dompriv)) {
                   3062:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   3063:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   3064:                 }
                   3065:                 if (($trest ne '') && (defined($coursepriv))) {
                   3066:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   3067:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   3068:                 }
                   3069:             }
                   3070:         }
                   3071:     }
                   3072: }
                   3073: 
1.678     raeburn  3074: sub group_roleprivs {
                   3075:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   3076:     my $access = 1;
                   3077:     my $now = time;
                   3078:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   3079:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   3080:     if ($access) {
1.811     albertel 3081:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  3082:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   3083:     }
                   3084: }
1.567     raeburn  3085: 
                   3086: sub standard_roleprivs {
                   3087:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   3088:     if (defined($pr{$trole.':s'})) {
                   3089:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   3090:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   3091:     }
                   3092:     if ($tdomain ne '') {
                   3093:         if (defined($pr{$trole.':d'})) {
                   3094:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   3095:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   3096:         }
                   3097:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   3098:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   3099:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   3100:         }
                   3101:     }
                   3102: }
                   3103: 
                   3104: sub set_userprivs {
1.678     raeburn  3105:     my ($userroles,$allroles,$allgroups) = @_; 
1.567     raeburn  3106:     my $author=0;
                   3107:     my $adv=0;
1.678     raeburn  3108:     my %grouproles = ();
                   3109:     if (keys(%{$allgroups}) > 0) {
                   3110:         foreach my $role (keys %{$allroles}) {
1.681     raeburn  3111:             my ($trole,$area,$sec,$extendedarea);
1.811     albertel 3112:             if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)-) {
1.678     raeburn  3113:                 $trole = $1;
                   3114:                 $area = $2;
1.681     raeburn  3115:                 $sec = $3;
                   3116:                 $extendedarea = $area.$sec;
                   3117:                 if (exists($$allgroups{$area})) {
                   3118:                     foreach my $group (keys(%{$$allgroups{$area}})) {
                   3119:                         my $spec = $trole.'.'.$extendedarea;
                   3120:                         $grouproles{$spec.'.'.$area.'/'.$group} = 
                   3121:                                                 $$allgroups{$area}{$group};
1.678     raeburn  3122:                     }
                   3123:                 }
                   3124:             }
                   3125:         }
                   3126:     }
1.800     albertel 3127:     foreach my $group (keys(%grouproles)) {
                   3128:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  3129:     }
1.800     albertel 3130:     foreach my $role (keys(%{$allroles})) {
                   3131:         my %thesepriv;
                   3132:         if (($role=~/^au/) || ($role=~/^ca/)) { $author=1; }
                   3133:         foreach my $item (split(/:/,$$allroles{$role})) {
                   3134:             if ($item ne '') {
                   3135:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  3136:                 if ($restrictions eq '') {
                   3137:                     $thesepriv{$privilege}='F';
                   3138:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   3139:                     $thesepriv{$privilege}.=$restrictions;
                   3140:                 }
                   3141:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
                   3142:             }
                   3143:         }
                   3144:         my $thesestr='';
1.800     albertel 3145:         foreach my $priv (keys(%thesepriv)) {
                   3146: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   3147: 	}
                   3148:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  3149:     }
                   3150:     return ($author,$adv);
                   3151: }
                   3152: 
1.12      www      3153: # --------------------------------------------------------------- get interface
                   3154: 
                   3155: sub get {
1.131     albertel 3156:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      3157:    my $items='';
1.800     albertel 3158:    foreach my $item (@$storearr) {
                   3159:        $items.=&escape($item).'&';
1.191     harris41 3160:    }
1.12      www      3161:    $items=~s/\&$//;
1.620     albertel 3162:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3163:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 3164:    my $uhome=&homeserver($uname,$udomain);
                   3165: 
1.133     albertel 3166:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      3167:    my @pairs=split(/\&/,$rep);
1.273     albertel 3168:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   3169:      return @pairs;
                   3170:    }
1.15      www      3171:    my %returnhash=();
1.42      www      3172:    my $i=0;
1.800     albertel 3173:    foreach my $item (@$storearr) {
                   3174:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      3175:       $i++;
1.191     harris41 3176:    }
1.15      www      3177:    return %returnhash;
1.27      www      3178: }
                   3179: 
                   3180: # --------------------------------------------------------------- del interface
                   3181: 
                   3182: sub del {
1.133     albertel 3183:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      3184:    my $items='';
1.800     albertel 3185:    foreach my $item (@$storearr) {
                   3186:        $items.=&escape($item).'&';
1.191     harris41 3187:    }
1.27      www      3188:    $items=~s/\&$//;
1.620     albertel 3189:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3190:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 3191:    my $uhome=&homeserver($uname,$udomain);
                   3192: 
                   3193:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      3194: }
                   3195: 
                   3196: # -------------------------------------------------------------- dump interface
                   3197: 
                   3198: sub dump {
1.755     albertel 3199:     my ($namespace,$udomain,$uname,$regexp,$range)=@_;
                   3200:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3201:     if (!$uname) { $uname=$env{'user.name'}; }
                   3202:     my $uhome=&homeserver($uname,$udomain);
                   3203:     if ($regexp) {
                   3204: 	$regexp=&escape($regexp);
                   3205:     } else {
                   3206: 	$regexp='.';
                   3207:     }
                   3208:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   3209:     my @pairs=split(/\&/,$rep);
                   3210:     my %returnhash=();
                   3211:     foreach my $item (@pairs) {
                   3212: 	my ($key,$value)=split(/=/,$item,2);
                   3213: 	$key = &unescape($key);
                   3214: 	next if ($key =~ /^error: 2 /);
                   3215: 	$returnhash{$key}=&thaw_unescape($value);
                   3216:     }
                   3217:     return %returnhash;
1.407     www      3218: }
                   3219: 
1.717     albertel 3220: # --------------------------------------------------------- dumpstore interface
                   3221: 
                   3222: sub dumpstore {
                   3223:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822     albertel 3224:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3225:    if (!$uname) { $uname=$env{'user.name'}; }
                   3226:    my $uhome=&homeserver($uname,$udomain);
                   3227:    if ($regexp) {
                   3228:        $regexp=&escape($regexp);
                   3229:    } else {
                   3230:        $regexp='.';
                   3231:    }
                   3232:    my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   3233:    my @pairs=split(/\&/,$rep);
                   3234:    my %returnhash=();
                   3235:    foreach my $item (@pairs) {
                   3236:        my ($key,$value)=split(/=/,$item,2);
                   3237:        next if ($key =~ /^error: 2 /);
                   3238:        $returnhash{$key}=&thaw_unescape($value);
                   3239:    }
                   3240:    return %returnhash;
1.717     albertel 3241: }
                   3242: 
1.407     www      3243: # -------------------------------------------------------------- keys interface
                   3244: 
                   3245: sub getkeys {
                   3246:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 3247:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3248:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      3249:    my $uhome=&homeserver($uname,$udomain);
                   3250:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   3251:    my @keyarray=();
1.800     albertel 3252:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  3253:       next if ($key =~ /^error: 2 /);
1.800     albertel 3254:       push(@keyarray,&unescape($key));
1.407     www      3255:    }
                   3256:    return @keyarray;
1.318     matthew  3257: }
                   3258: 
1.319     matthew  3259: # --------------------------------------------------------------- currentdump
                   3260: sub currentdump {
1.328     matthew  3261:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 3262:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   3263:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   3264:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  3265:    my $uhome = &homeserver($sname,$sdom);
                   3266:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318     matthew  3267:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  3268:    #
1.318     matthew  3269:    my %returnhash=();
1.319     matthew  3270:    #
                   3271:    if ($rep eq "unknown_cmd") { 
                   3272:        # an old lond will not know currentdump
                   3273:        # Do a dump and make it look like a currentdump
1.822     albertel 3274:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  3275:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   3276:        my %hash = @tmp;
                   3277:        @tmp=();
1.424     matthew  3278:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  3279:    } else {
                   3280:        my @pairs=split(/\&/,$rep);
1.800     albertel 3281:        foreach my $pair (@pairs) {
                   3282:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  3283:            my ($symb,$param) = split(/:/,$key);
                   3284:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 3285:                                                         &thaw_unescape($value);
1.319     matthew  3286:        }
1.191     harris41 3287:    }
1.12      www      3288:    return %returnhash;
1.424     matthew  3289: }
                   3290: 
                   3291: sub convert_dump_to_currentdump{
                   3292:     my %hash = %{shift()};
                   3293:     my %returnhash;
                   3294:     # Code ripped from lond, essentially.  The only difference
                   3295:     # here is the unescaping done by lonnet::dump().  Conceivably
                   3296:     # we might run in to problems with parameter names =~ /^v\./
                   3297:     while (my ($key,$value) = each(%hash)) {
                   3298:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 3299: 	$symb  = &unescape($symb);
                   3300: 	$param = &unescape($param);
1.424     matthew  3301:         next if ($v eq 'version' || $symb eq 'keys');
                   3302:         next if (exists($returnhash{$symb}) &&
                   3303:                  exists($returnhash{$symb}->{$param}) &&
                   3304:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   3305:         $returnhash{$symb}->{$param}=$value;
                   3306:         $returnhash{$symb}->{'v.'.$param}=$v;
                   3307:     }
                   3308:     #
                   3309:     # Remove all of the keys in the hashes which keep track of
                   3310:     # the version of the parameter.
                   3311:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   3312:         # use a foreach because we are going to delete from the hash.
                   3313:         foreach my $key (keys(%$param_hash)) {
                   3314:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   3315:         }
                   3316:     }
                   3317:     return \%returnhash;
1.12      www      3318: }
                   3319: 
1.627     albertel 3320: # ------------------------------------------------------ critical inc interface
                   3321: 
                   3322: sub cinc {
                   3323:     return &inc(@_,'critical');
                   3324: }
                   3325: 
1.449     matthew  3326: # --------------------------------------------------------------- inc interface
                   3327: 
                   3328: sub inc {
1.627     albertel 3329:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 3330:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3331:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  3332:     my $uhome=&homeserver($uname,$udomain);
                   3333:     my $items='';
                   3334:     if (! ref($store)) {
                   3335:         # got a single value, so use that instead
                   3336:         $items = &escape($store).'=&';
                   3337:     } elsif (ref($store) eq 'SCALAR') {
                   3338:         $items = &escape($$store).'=&';        
                   3339:     } elsif (ref($store) eq 'ARRAY') {
                   3340:         $items = join('=&',map {&escape($_);} @{$store});
                   3341:     } elsif (ref($store) eq 'HASH') {
                   3342:         while (my($key,$value) = each(%{$store})) {
                   3343:             $items.= &escape($key).'='.&escape($value).'&';
                   3344:         }
                   3345:     }
                   3346:     $items=~s/\&$//;
1.627     albertel 3347:     if ($critical) {
                   3348: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   3349:     } else {
                   3350: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   3351:     }
1.449     matthew  3352: }
                   3353: 
1.12      www      3354: # --------------------------------------------------------------- put interface
                   3355: 
                   3356: sub put {
1.134     albertel 3357:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 3358:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3359:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 3360:    my $uhome=&homeserver($uname,$udomain);
1.12      www      3361:    my $items='';
1.800     albertel 3362:    foreach my $item (keys(%$storehash)) {
                   3363:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 3364:    }
1.12      www      3365:    $items=~s/\&$//;
1.134     albertel 3366:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      3367: }
                   3368: 
1.631     albertel 3369: # ------------------------------------------------------------ newput interface
                   3370: 
                   3371: sub newput {
                   3372:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   3373:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3374:    if (!$uname) { $uname=$env{'user.name'}; }
                   3375:    my $uhome=&homeserver($uname,$udomain);
                   3376:    my $items='';
                   3377:    foreach my $key (keys(%$storehash)) {
                   3378:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   3379:    }
                   3380:    $items=~s/\&$//;
                   3381:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   3382: }
                   3383: 
                   3384: # ---------------------------------------------------------  putstore interface
                   3385: 
1.524     raeburn  3386: sub putstore {
1.715     albertel 3387:    my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620     albertel 3388:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3389:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  3390:    my $uhome=&homeserver($uname,$udomain);
                   3391:    my $items='';
1.715     albertel 3392:    foreach my $key (keys(%$storehash)) {
                   3393:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  3394:    }
1.715     albertel 3395:    $items=~s/\&$//;
1.716     albertel 3396:    my $esc_symb=&escape($symb);
                   3397:    my $esc_v=&escape($version);
1.715     albertel 3398:    my $reply =
1.716     albertel 3399:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 3400: 	      $uhome);
                   3401:    if ($reply eq 'unknown_cmd') {
1.716     albertel 3402:        # gfall back to way things use to be done
1.715     albertel 3403:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   3404: 			    $uname);
1.524     raeburn  3405:    }
1.715     albertel 3406:    return $reply;
                   3407: }
                   3408: 
                   3409: sub old_putstore {
1.716     albertel 3410:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   3411:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3412:     if (!$uname) { $uname=$env{'user.name'}; }
                   3413:     my $uhome=&homeserver($uname,$udomain);
                   3414:     my %newstorehash;
1.800     albertel 3415:     foreach my $item (keys(%$storehash)) {
                   3416: 	my $key = $version.':'.&escape($symb).':'.$item;
                   3417: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 3418:     }
                   3419:     my $items='';
                   3420:     my %allitems = ();
1.800     albertel 3421:     foreach my $item (keys(%newstorehash)) {
                   3422: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 3423: 	    my $key = $1.':keys:'.$2;
                   3424: 	    $allitems{$key} .= $3.':';
                   3425: 	}
1.800     albertel 3426: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 3427:     }
1.800     albertel 3428:     foreach my $item (keys(%allitems)) {
                   3429: 	$allitems{$item} =~ s/\:$//;
                   3430: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 3431:     }
                   3432:     $items=~s/\&$//;
                   3433:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  3434: }
                   3435: 
1.47      www      3436: # ------------------------------------------------------ critical put interface
                   3437: 
                   3438: sub cput {
1.134     albertel 3439:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 3440:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3441:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 3442:    my $uhome=&homeserver($uname,$udomain);
1.47      www      3443:    my $items='';
1.800     albertel 3444:    foreach my $item (keys(%$storehash)) {
                   3445:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 3446:    }
1.47      www      3447:    $items=~s/\&$//;
1.134     albertel 3448:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      3449: }
                   3450: 
                   3451: # -------------------------------------------------------------- eget interface
                   3452: 
                   3453: sub eget {
1.133     albertel 3454:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      3455:    my $items='';
1.800     albertel 3456:    foreach my $item (@$storearr) {
                   3457:        $items.=&escape($item).'&';
1.191     harris41 3458:    }
1.12      www      3459:    $items=~s/\&$//;
1.620     albertel 3460:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3461:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 3462:    my $uhome=&homeserver($uname,$udomain);
                   3463:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      3464:    my @pairs=split(/\&/,$rep);
                   3465:    my %returnhash=();
1.42      www      3466:    my $i=0;
1.800     albertel 3467:    foreach my $item (@$storearr) {
                   3468:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      3469:       $i++;
1.191     harris41 3470:    }
1.12      www      3471:    return %returnhash;
                   3472: }
                   3473: 
1.667     albertel 3474: # ------------------------------------------------------------ tmpput interface
                   3475: sub tmpput {
1.802     raeburn  3476:     my ($storehash,$server,$context)=@_;
1.667     albertel 3477:     my $items='';
1.800     albertel 3478:     foreach my $item (keys(%$storehash)) {
                   3479: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 3480:     }
                   3481:     $items=~s/\&$//;
1.802     raeburn  3482:     if (defined($context)) {
                   3483:         $items .= ':'.&escape($context);
                   3484:     }
1.667     albertel 3485:     return &reply("tmpput:$items",$server);
                   3486: }
                   3487: 
                   3488: # ------------------------------------------------------------ tmpget interface
                   3489: sub tmpget {
1.688     albertel 3490:     my ($token,$server)=@_;
                   3491:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   3492:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 3493:     my %returnhash;
                   3494:     foreach my $item (split(/\&/,$rep)) {
                   3495: 	my ($key,$value)=split(/=/,$item);
                   3496: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   3497:     }
                   3498:     return %returnhash;
                   3499: }
                   3500: 
1.688     albertel 3501: # ------------------------------------------------------------ tmpget interface
                   3502: sub tmpdel {
                   3503:     my ($token,$server)=@_;
                   3504:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   3505:     return &reply("tmpdel:$token",$server);
                   3506: }
                   3507: 
1.765     albertel 3508: # -------------------------------------------------- portfolio access checking
                   3509: 
                   3510: sub portfolio_access {
1.766     albertel 3511:     my ($requrl) = @_;
1.765     albertel 3512:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
                   3513:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814     raeburn  3514:     if ($result) {
                   3515:         my %setters;
                   3516:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   3517:             my ($startblock,$endblock) =
                   3518:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   3519:             if ($startblock && $endblock) {
                   3520:                 return 'B';
                   3521:             }
                   3522:         } else {
                   3523:             my ($startblock,$endblock) =
                   3524:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   3525:             if ($startblock && $endblock) {
                   3526:                 return 'B';
                   3527:             }
                   3528:         }
                   3529:     }
1.765     albertel 3530:     if ($result eq 'ok') {
1.766     albertel 3531:        return 'F';
1.765     albertel 3532:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 3533:        return 'A';
1.765     albertel 3534:     }
1.766     albertel 3535:     return '';
1.765     albertel 3536: }
                   3537: 
                   3538: sub get_portfolio_access {
1.767     albertel 3539:     my ($udom,$unum,$file_name,$group,$access_hash) = @_;
                   3540: 
                   3541:     if (!ref($access_hash)) {
                   3542: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   3543: 	my %access_controls = &get_access_controls($current_perms,$group,
                   3544: 						   $file_name);
                   3545: 	$access_hash = $access_controls{$file_name};
                   3546:     }
                   3547: 
1.765     albertel 3548:     my ($public,$guest,@domains,@users,@courses,@groups);
                   3549:     my $now = time;
                   3550:     if (ref($access_hash) eq 'HASH') {
                   3551:         foreach my $key (keys(%{$access_hash})) {
                   3552:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   3553:             if ($start > $now) {
                   3554:                 next;
                   3555:             }
                   3556:             if ($end && $end<$now) {
                   3557:                 next;
                   3558:             }
                   3559:             if ($scope eq 'public') {
                   3560:                 $public = $key;
                   3561:                 last;
                   3562:             } elsif ($scope eq 'guest') {
                   3563:                 $guest = $key;
                   3564:             } elsif ($scope eq 'domains') {
                   3565:                 push(@domains,$key);
                   3566:             } elsif ($scope eq 'users') {
                   3567:                 push(@users,$key);
                   3568:             } elsif ($scope eq 'course') {
                   3569:                 push(@courses,$key);
                   3570:             } elsif ($scope eq 'group') {
                   3571:                 push(@groups,$key);
                   3572:             }
                   3573:         }
                   3574:         if ($public) {
                   3575:             return 'ok';
                   3576:         }
                   3577:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   3578:             if ($guest) {
                   3579:                 return $guest;
                   3580:             }
                   3581:         } else {
                   3582:             if (@domains > 0) {
                   3583:                 foreach my $domkey (@domains) {
                   3584:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   3585:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   3586:                             return 'ok';
                   3587:                         }
                   3588:                     }
                   3589:                 }
                   3590:             }
                   3591:             if (@users > 0) {
                   3592:                 foreach my $userkey (@users) {
1.865     raeburn  3593:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   3594:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   3595:                             if (ref($item) eq 'HASH') {
                   3596:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   3597:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   3598:                                     return 'ok';
                   3599:                                 }
                   3600:                             }
                   3601:                         }
                   3602:                     } 
1.765     albertel 3603:                 }
                   3604:             }
                   3605:             my %roleshash;
                   3606:             my @courses_and_groups = @courses;
                   3607:             push(@courses_and_groups,@groups); 
                   3608:             if (@courses_and_groups > 0) {
                   3609:                 my (%allgroups,%allroles); 
                   3610:                 my ($start,$end,$role,$sec,$group);
                   3611:                 foreach my $envkey (%env) {
1.811     albertel 3612:                     if ($envkey =~ m-^user\.role\.(gr|cc|in|ta|ep|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 3613:                         my $cid = $2.'_'.$3; 
                   3614:                         if ($1 eq 'gr') {
                   3615:                             $group = $4;
                   3616:                             $allgroups{$cid}{$group} = $env{$envkey};
                   3617:                         } else {
                   3618:                             if ($4 eq '') {
                   3619:                                 $sec = 'none';
                   3620:                             } else {
                   3621:                                 $sec = $4;
                   3622:                             }
                   3623:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   3624:                         }
1.811     albertel 3625:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 3626:                         my $cid = $2.'_'.$3;
                   3627:                         if ($4 eq '') {
                   3628:                             $sec = 'none';
                   3629:                         } else {
                   3630:                             $sec = $4;
                   3631:                         }
                   3632:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   3633:                     }
                   3634:                 }
                   3635:                 if (keys(%allroles) == 0) {
                   3636:                     return;
                   3637:                 }
                   3638:                 foreach my $key (@courses_and_groups) {
                   3639:                     my %content = %{$$access_hash{$key}};
                   3640:                     my $cnum = $content{'number'};
                   3641:                     my $cdom = $content{'domain'};
                   3642:                     my $cid = $cdom.'_'.$cnum;
                   3643:                     if (!exists($allroles{$cid})) {
                   3644:                         next;
                   3645:                     }    
                   3646:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   3647:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   3648:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   3649:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   3650:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   3651:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   3652:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   3653:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   3654:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   3655:                                         if (grep/^all$/,@sections) {
                   3656:                                             return 'ok';
                   3657:                                         } else {
                   3658:                                             if (grep/^$sec$/,@sections) {
                   3659:                                                 return 'ok';
                   3660:                                             }
                   3661:                                         }
                   3662:                                     }
                   3663:                                 }
                   3664:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   3665:                                     if (grep/^none$/,@groups) {
                   3666:                                         return 'ok';
                   3667:                                     }
                   3668:                                 } else {
                   3669:                                     if (grep/^all$/,@groups) {
                   3670:                                         return 'ok';
                   3671:                                     } 
                   3672:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   3673:                                         if (grep/^$group$/,@groups) {
                   3674:                                             return 'ok';
                   3675:                                         }
                   3676:                                     }
                   3677:                                 } 
                   3678:                             }
                   3679:                         }
                   3680:                     }
                   3681:                 }
                   3682:             }
                   3683:             if ($guest) {
                   3684:                 return $guest;
                   3685:             }
                   3686:         }
                   3687:     }
                   3688:     return;
                   3689: }
                   3690: 
                   3691: sub course_group_datechecker {
                   3692:     my ($dates,$now,$status) = @_;
                   3693:     my ($start,$end) = split(/\./,$dates);
                   3694:     if (!$start && !$end) {
                   3695:         return 'ok';
                   3696:     }
                   3697:     if (grep/^active$/,@{$status}) {
                   3698:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   3699:             return 'ok';
                   3700:         }
                   3701:     }
                   3702:     if (grep/^previous$/,@{$status}) {
                   3703:         if ($end > $now ) {
                   3704:             return 'ok';
                   3705:         }
                   3706:     }
                   3707:     if (grep/^future$/,@{$status}) {
                   3708:         if ($start > $now) {
                   3709:             return 'ok';
                   3710:         }
                   3711:     }
                   3712:     return; 
                   3713: }
                   3714: 
                   3715: sub parse_portfolio_url {
                   3716:     my ($url) = @_;
                   3717: 
                   3718:     my ($type,$udom,$unum,$group,$file_name);
                   3719:     
1.823     albertel 3720:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 3721: 	$type = 1;
                   3722:         $udom = $1;
                   3723:         $unum = $2;
                   3724:         $file_name = $3;
1.823     albertel 3725:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 3726: 	$type = 2;
                   3727:         $udom = $1;
                   3728:         $unum = $2;
                   3729:         $group = $3;
                   3730:         $file_name = $3.'/'.$4;
                   3731:     }
                   3732:     if (wantarray) {
                   3733: 	return ($type,$udom,$unum,$file_name,$group);
                   3734:     }
                   3735:     return $type;
                   3736: }
                   3737: 
                   3738: sub is_portfolio_url {
                   3739:     my ($url) = @_;
                   3740:     return scalar(&parse_portfolio_url($url));
                   3741: }
                   3742: 
1.798     raeburn  3743: sub is_portfolio_file {
                   3744:     my ($file) = @_;
1.820     raeburn  3745:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  3746:         return 1;
                   3747:     }
                   3748:     return;
                   3749: }
                   3750: 
                   3751: 
1.341     www      3752: # ---------------------------------------------- Custom access rule evaluation
                   3753: 
                   3754: sub customaccess {
                   3755:     my ($priv,$uri)=@_;
1.807     albertel 3756:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      3757:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 3758:     $udom = &LONCAPA::clean_domain($udom);
                   3759:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      3760:     my $access=0;
1.800     albertel 3761:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
                   3762: 	my ($effect,$realm,$role)=split(/\:/,$right);
1.343     www      3763:         if ($role) {
                   3764: 	   if ($role ne $urole) { next; }
                   3765:         }
1.800     albertel 3766:         foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   3767:             my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
1.343     www      3768:             if ($tdom) {
                   3769: 		if ($tdom ne $udom) { next; }
                   3770:             }
                   3771:             if ($tcrs) {
                   3772: 		if ($tcrs ne $ucrs) { next; }
                   3773:             }
                   3774:             if ($tsec) {
                   3775: 		if ($tsec ne $usec) { next; }
                   3776:             }
                   3777:             $access=($effect eq 'allow');
                   3778:             last;
1.342     www      3779:         }
1.402     bowersj2 3780: 	if ($realm eq '' && $role eq '') {
                   3781:             $access=($effect eq 'allow');
                   3782: 	}
1.341     www      3783:     }
                   3784:     return $access;
                   3785: }
                   3786: 
1.103     harris41 3787: # ------------------------------------------------- Check for a user privilege
1.12      www      3788: 
                   3789: sub allowed {
1.810     raeburn  3790:     my ($priv,$uri,$symb,$role)=@_;
1.705     albertel 3791:     my $ver_orguri=$uri;
1.439     www      3792:     $uri=&deversion($uri);
1.152     www      3793:     my $orguri=$uri;
1.52      www      3794:     $uri=&declutter($uri);
1.809     raeburn  3795: 
1.810     raeburn  3796:     if ($priv eq 'evb') {
                   3797: # Evade communication block restrictions for specified role in a course
                   3798:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   3799:             return $1;
                   3800:         } else {
                   3801:             return;
                   3802:         }
                   3803:     }
                   3804: 
1.620     albertel 3805:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      3806: # Free bre access to adm and meta resources
1.775     albertel 3807:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$})) 
1.769     albertel 3808: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   3809: 	&& ($priv eq 'bre')) {
1.14      www      3810: 	return 'F';
1.159     www      3811:     }
                   3812: 
1.545     banghart 3813: # Free bre access to user's own portfolio contents
1.714     raeburn  3814:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  3815:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  3816: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  3817:         my %setters;
                   3818:         my ($startblock,$endblock) = 
                   3819:             &Apache::loncommon::blockcheck(\%setters,'port');
                   3820:         if ($startblock && $endblock) {
                   3821:             return 'B';
                   3822:         } else {
                   3823:             return 'F';
                   3824:         }
1.545     banghart 3825:     }
                   3826: 
1.762     raeburn  3827: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  3828:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   3829:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   3830:         if (exists($env{'request.course.id'})) {
                   3831:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3832:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   3833:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   3834:                 my $courseprivid=$env{'request.course.id'};
                   3835:                 $courseprivid=~s/\_/\//;
                   3836:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   3837:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   3838:                     return $1; 
1.762     raeburn  3839:                 } else {
                   3840:                     if ($env{'request.course.sec'}) {
                   3841:                         $courseprivid.='/'.$env{'request.course.sec'};
                   3842:                     }
                   3843:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   3844:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   3845:                         return $2;
                   3846:                     }
1.714     raeburn  3847:                 }
                   3848:             }
                   3849:         }
                   3850:     }
                   3851: 
1.159     www      3852: # Free bre to public access
                   3853: 
                   3854:     if ($priv eq 'bre') {
1.238     www      3855:         my $copyright=&metadata($uri,'copyright');
1.620     albertel 3856: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      3857:            return 'F'; 
                   3858:         }
1.238     www      3859:         if ($copyright eq 'priv') {
                   3860:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 3861: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      3862: 		return '';
                   3863:             }
                   3864:         }
                   3865:         if ($copyright eq 'domain') {
                   3866:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 3867: 	    unless (($env{'user.domain'} eq $1) ||
                   3868:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      3869: 		return '';
                   3870:             }
1.262     matthew  3871:         }
1.620     albertel 3872:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  3873:             # Library role, so allow browsing of resources in this domain.
                   3874:             return 'F';
1.238     www      3875:         }
1.341     www      3876:         if ($copyright eq 'custom') {
                   3877: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   3878:         }
1.14      www      3879:     }
1.264     matthew  3880:     # Domain coordinator is trying to create a course
1.620     albertel 3881:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  3882:         # uri is the requested domain in this case.
                   3883:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  3884:         # a role of dc for the domain in question.
1.620     albertel 3885:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  3886:     }
1.29      www      3887: 
1.52      www      3888:     my $thisallowed='';
                   3889:     my $statecond=0;
                   3890:     my $courseprivid='';
                   3891: 
                   3892: # Course
                   3893: 
1.620     albertel 3894:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.52      www      3895:        $thisallowed.=$1;
                   3896:     }
1.29      www      3897: 
1.52      www      3898: # Domain
                   3899: 
1.620     albertel 3900:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 3901:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      3902:        $thisallowed.=$1;
                   3903:     }
1.52      www      3904: 
                   3905: # Course: uri itself is a course
1.66      www      3906:     my $courseuri=$uri;
                   3907:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      3908:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      3909: 
1.620     albertel 3910:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 3911:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      3912:        $thisallowed.=$1;
                   3913:     }
1.29      www      3914: 
1.665     albertel 3915: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 3916: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 3917:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 3918: 	$thisallowed='';
1.671     raeburn  3919:         my ($match)=&is_on_map($uri);
                   3920:         if ($match) {
                   3921:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   3922:                   =~/\Q$priv\E\&([^\:]*)/) {
                   3923:                 $thisallowed.=$1;
                   3924:             }
                   3925:         } else {
1.705     albertel 3926:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  3927:             if ($refuri) {
                   3928:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  3929:                     $thisallowed='F';
1.671     raeburn  3930:                 } else {
                   3931:                     $refuri=&declutter($refuri);
                   3932:                     my ($match) = &is_on_map($refuri);
                   3933:                     if ($match) {
                   3934:                         $thisallowed='F';
                   3935:                     }
1.669     raeburn  3936:                 }
1.671     raeburn  3937:             }
                   3938:         }
1.314     www      3939:     }
1.492     albertel 3940: 
1.766     albertel 3941:     if ($priv eq 'bre'
                   3942: 	&& $thisallowed ne 'F' 
                   3943: 	&& $thisallowed ne '2'
                   3944: 	&& &is_portfolio_url($uri)) {
                   3945: 	$thisallowed = &portfolio_access($uri);
                   3946:     }
                   3947:     
1.52      www      3948: # Full access at system, domain or course-wide level? Exit.
1.29      www      3949: 
                   3950:     if ($thisallowed=~/F/) {
                   3951: 	return 'F';
                   3952:     }
                   3953: 
1.52      www      3954: # If this is generating or modifying users, exit with special codes
1.29      www      3955: 
1.643     www      3956:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   3957: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 3958: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      3959: # no author name given, so this just checks on the general right to make a co-author in this domain
                   3960: 	    unless ($auname) { return $thisallowed; }
                   3961: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 3962: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   3963: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   3964: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   3965: 	}
1.52      www      3966: 	return $thisallowed;
                   3967:     }
                   3968: #
1.103     harris41 3969: # Gathered so far: system, domain and course wide privileges
1.52      www      3970: #
                   3971: # Course: See if uri or referer is an individual resource that is part of 
                   3972: # the course
                   3973: 
1.620     albertel 3974:     if ($env{'request.course.id'}) {
1.232     www      3975: 
1.620     albertel 3976:        $courseprivid=$env{'request.course.id'};
                   3977:        if ($env{'request.course.sec'}) {
                   3978:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      3979:        }
                   3980:        $courseprivid=~s/\_/\//;
                   3981:        my $checkreferer=1;
1.232     www      3982:        my ($match,$cond)=&is_on_map($uri);
                   3983:        if ($match) {
                   3984:            $statecond=$cond;
1.620     albertel 3985:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 3986:                =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      3987:                $thisallowed.=$1;
                   3988:                $checkreferer=0;
                   3989:            }
1.29      www      3990:        }
1.83      www      3991:        
1.148     www      3992:        if ($checkreferer) {
1.620     albertel 3993: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      3994:             unless ($refuri) {
1.800     albertel 3995:                 foreach my $key (keys(%env)) {
                   3996: 		    if ($key=~/^httpref\..*\*/) {
                   3997: 			my $pattern=$key;
1.156     www      3998:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      3999:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   4000:                         $pattern=~s/\//\\\//g;
1.152     www      4001:                         if ($orguri=~/$pattern/) {
1.800     albertel 4002: 			    $refuri=$env{$key};
1.148     www      4003:                         }
                   4004:                     }
1.191     harris41 4005:                 }
1.148     www      4006:             }
1.232     www      4007: 
1.148     www      4008:          if ($refuri) { 
1.152     www      4009: 	  $refuri=&declutter($refuri);
1.232     www      4010:           my ($match,$cond)=&is_on_map($refuri);
                   4011:             if ($match) {
                   4012:               my $refstatecond=$cond;
1.620     albertel 4013:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 4014:                   =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4015:                   $thisallowed.=$1;
1.53      www      4016:                   $uri=$refuri;
                   4017:                   $statecond=$refstatecond;
1.52      www      4018:               }
                   4019:           }
1.148     www      4020:         }
1.29      www      4021:        }
1.52      www      4022:    }
1.29      www      4023: 
1.52      www      4024: #
1.103     harris41 4025: # Gathered now: all privileges that could apply, and condition number
1.52      www      4026: # 
                   4027: #
                   4028: # Full or no access?
                   4029: #
1.29      www      4030: 
1.52      www      4031:     if ($thisallowed=~/F/) {
                   4032: 	return 'F';
                   4033:     }
1.29      www      4034: 
1.52      www      4035:     unless ($thisallowed) {
                   4036:         return '';
                   4037:     }
1.29      www      4038: 
1.52      www      4039: # Restrictions exist, deal with them
                   4040: #
                   4041: #   C:according to course preferences
                   4042: #   R:according to resource settings
                   4043: #   L:unless locked
                   4044: #   X:according to user session state
                   4045: #
                   4046: 
                   4047: # Possibly locked functionality, check all courses
1.54      www      4048: # Locks might take effect only after 10 minutes cache expiration for other
                   4049: # courses, and 2 minutes for current course
1.52      www      4050: 
                   4051:     my $envkey;
                   4052:     if ($thisallowed=~/L/) {
1.620     albertel 4053:         foreach $envkey (keys %env) {
1.54      www      4054:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   4055:                my $courseid=$2;
                   4056:                my $roleid=$1.'.'.$2;
1.92      www      4057:                $courseid=~s/^\///;
1.54      www      4058:                my $expiretime=600;
1.620     albertel 4059:                if ($env{'request.role'} eq $roleid) {
1.54      www      4060: 		  $expiretime=120;
                   4061:                }
                   4062: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   4063:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 4064:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 4065: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      4066:                }
1.620     albertel 4067:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   4068:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   4069: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   4070:                        &log($env{'user.domain'},$env{'user.name'},
                   4071:                             $env{'user.home'},
1.57      www      4072:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      4073:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 4074:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      4075: 		       return '';
                   4076:                    }
                   4077:                }
1.620     albertel 4078:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   4079:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   4080: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   4081:                        &log($env{'user.domain'},$env{'user.name'},
                   4082:                             $env{'user.home'},
1.57      www      4083:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      4084:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 4085:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      4086: 		       return '';
                   4087:                    }
                   4088:                }
                   4089: 	   }
1.29      www      4090:        }
1.52      www      4091:     }
                   4092:    
                   4093: #
                   4094: # Rest of the restrictions depend on selected course
                   4095: #
                   4096: 
1.620     albertel 4097:     unless ($env{'request.course.id'}) {
1.766     albertel 4098: 	if ($thisallowed eq 'A') {
                   4099: 	    return 'A';
1.814     raeburn  4100:         } elsif ($thisallowed eq 'B') {
                   4101:             return 'B';
1.766     albertel 4102: 	} else {
                   4103: 	    return '1';
                   4104: 	}
1.52      www      4105:     }
1.29      www      4106: 
1.52      www      4107: #
                   4108: # Now user is definitely in a course
                   4109: #
1.53      www      4110: 
                   4111: 
                   4112: # Course preferences
                   4113: 
                   4114:    if ($thisallowed=~/C/) {
1.620     albertel 4115:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   4116:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   4117:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 4118: 	   =~/\Q$rolecode\E/) {
1.689     albertel 4119: 	   if ($priv ne 'pch') { 
                   4120: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   4121: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   4122: 			$env{'request.course.id'});
                   4123: 	   }
1.237     www      4124:            return '';
                   4125:        }
                   4126: 
1.620     albertel 4127:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 4128: 	   =~/\Q$unamedom\E/) {
1.689     albertel 4129: 	   if ($priv ne 'pch') { 
                   4130: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   4131: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   4132: 			$env{'request.course.id'});
                   4133: 	   }
1.54      www      4134:            return '';
                   4135:        }
1.53      www      4136:    }
                   4137: 
                   4138: # Resource preferences
                   4139: 
                   4140:    if ($thisallowed=~/R/) {
1.620     albertel 4141:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 4142:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.689     albertel 4143: 	   if ($priv ne 'pch') { 
                   4144: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   4145: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   4146: 	   }
                   4147: 	   return '';
1.54      www      4148:        }
1.53      www      4149:    }
1.30      www      4150: 
1.246     www      4151: # Restricted by state or randomout?
1.30      www      4152: 
1.52      www      4153:    if ($thisallowed=~/X/) {
1.620     albertel 4154:       if ($env{'acc.randomout'}) {
1.579     albertel 4155: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 4156:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      4157:             return ''; 
                   4158:          }
1.247     www      4159:       }
                   4160:       if (&condval($statecond)) {
1.52      www      4161: 	 return '2';
                   4162:       } else {
                   4163:          return '';
                   4164:       }
                   4165:    }
1.30      www      4166: 
1.766     albertel 4167:     if ($thisallowed eq 'A') {
                   4168: 	return 'A';
1.814     raeburn  4169:     } elsif ($thisallowed eq 'B') {
                   4170:         return 'B';
1.766     albertel 4171:     }
1.52      www      4172:    return 'F';
1.232     www      4173: }
                   4174: 
1.710     albertel 4175: sub split_uri_for_cond {
                   4176:     my $uri=&deversion(&declutter(shift));
                   4177:     my @uriparts=split(/\//,$uri);
                   4178:     my $filename=pop(@uriparts);
                   4179:     my $pathname=join('/',@uriparts);
                   4180:     return ($pathname,$filename);
                   4181: }
1.232     www      4182: # --------------------------------------------------- Is a resource on the map?
                   4183: 
                   4184: sub is_on_map {
1.710     albertel 4185:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 4186:     #Trying to find the conditional for the file
1.620     albertel 4187:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 4188: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      4189:     if ($match) {
1.289     bowersj2 4190: 	return (1,$1);
                   4191:     } else {
1.434     www      4192: 	return (0,0);
1.289     bowersj2 4193:     }
1.12      www      4194: }
                   4195: 
1.427     www      4196: # --------------------------------------------------------- Get symb from alias
                   4197: 
                   4198: sub get_symb_from_alias {
                   4199:     my $symb=shift;
                   4200:     my ($map,$resid,$url)=&decode_symb($symb);
                   4201: # Already is a symb
                   4202:     if ($url) { return $symb; }
                   4203: # Must be an alias
                   4204:     my $aliassymb='';
                   4205:     my %bighash;
1.620     albertel 4206:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      4207:                             &GDBM_READER(),0640)) {
                   4208:         my $rid=$bighash{'mapalias_'.$symb};
                   4209: 	if ($rid) {
                   4210: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 4211: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   4212: 				    $resid,$bighash{'src_'.$rid});
1.427     www      4213: 	}
                   4214:         untie %bighash;
                   4215:     }
                   4216:     return $aliassymb;
                   4217: }
                   4218: 
1.12      www      4219: # ----------------------------------------------------------------- Define Role
                   4220: 
                   4221: sub definerole {
                   4222:   if (allowed('mcr','/')) {
                   4223:     my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800     albertel 4224:     foreach my $role (split(':',$sysrole)) {
                   4225: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4226:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   4227:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   4228: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      4229:                return "refused:s:$crole&$cqual"; 
                   4230:             }
                   4231:         }
1.191     harris41 4232:     }
1.800     albertel 4233:     foreach my $role (split(':',$domrole)) {
                   4234: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4235:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   4236:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   4237: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      4238:                return "refused:d:$crole&$cqual"; 
                   4239:             }
                   4240:         }
1.191     harris41 4241:     }
1.800     albertel 4242:     foreach my $role (split(':',$courole)) {
                   4243: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4244:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   4245:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   4246: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      4247:                return "refused:c:$crole&$cqual"; 
                   4248:             }
                   4249:         }
1.191     harris41 4250:     }
1.620     albertel 4251:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
                   4252:                 "$env{'user.domain'}:$env{'user.name'}:".
1.21      www      4253: 	        "rolesdef_$rolename=".
                   4254:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.620     albertel 4255:     return reply($command,$env{'user.home'});
1.12      www      4256:   } else {
                   4257:     return 'refused';
                   4258:   }
1.105     harris41 4259: }
                   4260: 
                   4261: # ---------------- Make a metadata query against the network of library servers
                   4262: 
                   4263: sub metadata_query {
1.244     matthew  4264:     my ($query,$custom,$customshow,$server_array)=@_;
1.120     harris41 4265:     my %rhash;
1.845     albertel 4266:     my %libserv = &all_library();
1.244     matthew  4267:     my @server_list = (defined($server_array) ? @$server_array
                   4268:                                               : keys(%libserv) );
                   4269:     for my $server (@server_list) {
1.118     harris41 4270: 	unless ($custom or $customshow) {
                   4271: 	    my $reply=&reply("querysend:".&escape($query),$server);
                   4272: 	    $rhash{$server}=$reply;
                   4273: 	}
                   4274: 	else {
                   4275: 	    my $reply=&reply("querysend:".&escape($query).':'.
                   4276: 			     &escape($custom).':'.&escape($customshow),
                   4277: 			     $server);
                   4278: 	    $rhash{$server}=$reply;
                   4279: 	}
1.112     harris41 4280:     }
1.118     harris41 4281:     return \%rhash;
1.240     www      4282: }
                   4283: 
                   4284: # ----------------------------------------- Send log queries and wait for reply
                   4285: 
                   4286: sub log_query {
                   4287:     my ($uname,$udom,$query,%filters)=@_;
                   4288:     my $uhome=&homeserver($uname,$udom);
                   4289:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 4290:     my $uhost=&hostname($uhome);
1.800     albertel 4291:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      4292:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   4293:                        $uhome);
1.479     albertel 4294:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      4295:     return get_query_reply($queryid);
                   4296: }
                   4297: 
1.818     raeburn  4298: # -------------------------- Update MySQL table for portfolio file
                   4299: 
                   4300: sub update_portfolio_table {
1.821     raeburn  4301:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.818     raeburn  4302:     my $homeserver = &homeserver($uname,$udom);
                   4303:     my $queryid=
1.821     raeburn  4304:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   4305:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  4306:     my $reply = &get_query_reply($queryid);
                   4307:     return $reply;
                   4308: }
                   4309: 
1.508     raeburn  4310: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  4311: 
                   4312: sub fetch_enrollment_query {
1.511     raeburn  4313:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508     raeburn  4314:     my $homeserver;
1.547     raeburn  4315:     my $maxtries = 1;
1.508     raeburn  4316:     if ($context eq 'automated') {
                   4317:         $homeserver = $perlvar{'lonHostID'};
1.547     raeburn  4318:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  4319:     } else {
                   4320:         $homeserver = &homeserver($cnum,$dom);
                   4321:     }
1.838     albertel 4322:     my $host=&hostname($homeserver);
1.506     raeburn  4323:     my $cmd = '';
1.800     albertel 4324:     foreach my $affiliate (keys %{$affiliatesref}) {
                   4325:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  4326:     }
                   4327:     $cmd =~ s/%%$//;
                   4328:     $cmd = &escape($cmd);
                   4329:     my $query = 'fetchenrollment';
1.620     albertel 4330:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  4331:     unless ($queryid=~/^\Q$host\E\_/) { 
                   4332:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   4333:         return 'error: '.$queryid;
                   4334:     }
1.506     raeburn  4335:     my $reply = &get_query_reply($queryid);
1.547     raeburn  4336:     my $tries = 1;
                   4337:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   4338:         $reply = &get_query_reply($queryid);
                   4339:         $tries ++;
                   4340:     }
1.526     raeburn  4341:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 4342:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  4343:     } else {
1.515     raeburn  4344:         my @responses = split/:/,$reply;
                   4345:         if ($homeserver eq $perlvar{'lonHostID'}) {
1.800     albertel 4346:             foreach my $line (@responses) {
                   4347:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  4348:                 $$replyref{$key} = $value;
                   4349:             }
                   4350:         } else {
1.506     raeburn  4351:             my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800     albertel 4352:             foreach my $line (@responses) {
                   4353:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  4354:                 $$replyref{$key} = $value;
                   4355:                 if ($value > 0) {
1.800     albertel 4356:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   4357:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  4358:                         my $destname = $pathname.'/'.$filename;
                   4359:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  4360:                         if ($xml_classlist =~ /^error/) {
                   4361:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   4362:                         } else {
1.506     raeburn  4363:                             if ( open(FILE,">$destname") ) {
                   4364:                                 print FILE &unescape($xml_classlist);
                   4365:                                 close(FILE);
1.526     raeburn  4366:                             } else {
                   4367:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  4368:                             }
                   4369:                         }
                   4370:                     }
                   4371:                 }
                   4372:             }
                   4373:         }
                   4374:         return 'ok';
                   4375:     }
                   4376:     return 'error';
                   4377: }
                   4378: 
1.242     www      4379: sub get_query_reply {
                   4380:     my $queryid=shift;
1.240     www      4381:     my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
                   4382:     my $reply='';
                   4383:     for (1..100) {
                   4384: 	sleep 2;
                   4385:         if (-e $replyfile.'.end') {
1.448     albertel 4386: 	    if (open(my $fh,$replyfile)) {
1.240     www      4387:                $reply.=<$fh>;
1.448     albertel 4388:                close($fh);
1.240     www      4389: 	   } else { return 'error: reply_file_error'; }
1.242     www      4390:            return &unescape($reply);
                   4391: 	}
1.240     www      4392:     }
1.242     www      4393:     return 'timeout:'.$queryid;
1.240     www      4394: }
                   4395: 
                   4396: sub courselog_query {
1.241     www      4397: #
                   4398: # possible filters:
                   4399: # url: url or symb
                   4400: # username
                   4401: # domain
                   4402: # action: view, submit, grade
                   4403: # start: timestamp
                   4404: # end: timestamp
                   4405: #
1.240     www      4406:     my (%filters)=@_;
1.620     albertel 4407:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      4408:     if ($filters{'url'}) {
                   4409: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   4410:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   4411:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   4412:     }
1.620     albertel 4413:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4414:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      4415:     return &log_query($cname,$cdom,'courselog',%filters);
                   4416: }
                   4417: 
                   4418: sub userlog_query {
1.858     raeburn  4419: #
                   4420: # possible filters:
                   4421: # action: log check role
                   4422: # start: timestamp
                   4423: # end: timestamp
                   4424: #
1.240     www      4425:     my ($uname,$udom,%filters)=@_;
                   4426:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      4427: }
                   4428: 
1.506     raeburn  4429: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   4430: 
                   4431: sub auto_run {
1.508     raeburn  4432:     my ($cnum,$cdom) = @_;
                   4433:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  4434:     my $response = &reply('autorun:'.$cdom,$homeserver);
1.506     raeburn  4435:     return $response;
                   4436: }
1.776     albertel 4437: 
1.506     raeburn  4438: sub auto_get_sections {
1.508     raeburn  4439:     my ($cnum,$cdom,$inst_coursecode) = @_;
                   4440:     my $homeserver = &homeserver($cnum,$cdom);
1.506     raeburn  4441:     my @secs = ();
1.511     raeburn  4442:     my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
1.506     raeburn  4443:     unless ($response eq 'refused') {
                   4444:         @secs = split/:/,$response;
                   4445:     }
                   4446:     return @secs;
                   4447: }
1.776     albertel 4448: 
1.506     raeburn  4449: sub auto_new_course {
1.508     raeburn  4450:     my ($cnum,$cdom,$inst_course_id,$owner) = @_;
                   4451:     my $homeserver = &homeserver($cnum,$cdom);
1.515     raeburn  4452:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
1.506     raeburn  4453:     return $response;
                   4454: }
1.776     albertel 4455: 
1.506     raeburn  4456: sub auto_validate_courseID {
1.508     raeburn  4457:     my ($cnum,$cdom,$inst_course_id) = @_;
                   4458:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  4459:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  4460:     return $response;
                   4461: }
1.776     albertel 4462: 
1.506     raeburn  4463: sub auto_create_password {
1.508     raeburn  4464:     my ($cnum,$cdom,$authparam) = @_;
                   4465:     my $homeserver = &homeserver($cnum,$cdom); 
1.506     raeburn  4466:     my $create_passwd = 0;
                   4467:     my $authchk = '';
1.511     raeburn  4468:     my $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
1.506     raeburn  4469:     if ($response eq 'refused') {
                   4470:         $authchk = 'refused';
                   4471:     } else {
                   4472:         ($authparam,$create_passwd,$authchk) = split/:/,$response;
                   4473:     }
                   4474:     return ($authparam,$create_passwd,$authchk);
                   4475: }
                   4476: 
1.706     raeburn  4477: sub auto_photo_permission {
                   4478:     my ($cnum,$cdom,$students) = @_;
                   4479:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 4480:     my ($outcome,$perm_reqd,$conditions) = 
                   4481: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 4482:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4483: 	return (undef,undef);
                   4484:     }
1.706     raeburn  4485:     return ($outcome,$perm_reqd,$conditions);
                   4486: }
                   4487: 
                   4488: sub auto_checkphotos {
                   4489:     my ($uname,$udom,$pid) = @_;
                   4490:     my $homeserver = &homeserver($uname,$udom);
                   4491:     my ($result,$resulttype);
                   4492:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 4493: 				   &escape($uname).':'.&escape($pid),
                   4494: 				   $homeserver));
1.709     albertel 4495:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4496: 	return (undef,undef);
                   4497:     }
1.706     raeburn  4498:     if ($outcome) {
                   4499:         ($result,$resulttype) = split(/:/,$outcome);
                   4500:     } 
                   4501:     return ($result,$resulttype);
                   4502: }
                   4503: 
                   4504: sub auto_photochoice {
                   4505:     my ($cnum,$cdom) = @_;
                   4506:     my $homeserver = &homeserver($cnum,$cdom);
                   4507:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 4508: 						       &escape($cdom),
                   4509: 						       $homeserver)));
1.709     albertel 4510:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4511: 	return (undef,undef);
                   4512:     }
1.706     raeburn  4513:     return ($update,$comment);
                   4514: }
                   4515: 
                   4516: sub auto_photoupdate {
                   4517:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   4518:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 4519:     my $host=&hostname($homeserver);
1.706     raeburn  4520:     my $cmd = '';
                   4521:     my $maxtries = 1;
1.800     albertel 4522:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   4523:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  4524:     }
                   4525:     $cmd =~ s/%%$//;
                   4526:     $cmd = &escape($cmd);
                   4527:     my $query = 'institutionalphotos';
                   4528:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   4529:     unless ($queryid=~/^\Q$host\E\_/) {
                   4530:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   4531:         return 'error: '.$queryid;
                   4532:     }
                   4533:     my $reply = &get_query_reply($queryid);
                   4534:     my $tries = 1;
                   4535:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   4536:         $reply = &get_query_reply($queryid);
                   4537:         $tries ++;
                   4538:     }
                   4539:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   4540:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   4541:     } else {
                   4542:         my @responses = split(/:/,$reply);
                   4543:         my $outcome = shift(@responses); 
                   4544:         foreach my $item (@responses) {
                   4545:             my ($key,$value) = split(/=/,$item);
                   4546:             $$photo{$key} = $value;
                   4547:         }
                   4548:         return $outcome;
                   4549:     }
                   4550:     return 'error';
                   4551: }
                   4552: 
1.521     raeburn  4553: sub auto_instcode_format {
1.793     albertel 4554:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   4555: 	$cat_order) = @_;
1.521     raeburn  4556:     my $courses = '';
1.772     raeburn  4557:     my @homeservers;
1.521     raeburn  4558:     if ($caller eq 'global') {
1.841     albertel 4559: 	my %servers = &get_servers($codedom,'library');
                   4560: 	foreach my $tryserver (keys(%servers)) {
                   4561: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   4562: 		push(@homeservers,$tryserver);
                   4563: 	    }
1.584     raeburn  4564:         }
1.521     raeburn  4565:     } else {
1.772     raeburn  4566:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  4567:     }
1.793     albertel 4568:     foreach my $code (keys(%{$instcodes})) {
                   4569:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  4570:     }
                   4571:     chop($courses);
1.772     raeburn  4572:     my $ok_response = 0;
                   4573:     my $response;
                   4574:     while (@homeservers > 0 && $ok_response == 0) {
                   4575:         my $server = shift(@homeservers); 
                   4576:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   4577:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   4578:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.793     albertel 4579: 		split/:/,$response;
1.772     raeburn  4580:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   4581:             push(@{$codetitles},&str2array($codetitles_str));
                   4582:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   4583:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   4584:             $ok_response = 1;
                   4585:         }
                   4586:     }
                   4587:     if ($ok_response) {
1.521     raeburn  4588:         return 'ok';
1.772     raeburn  4589:     } else {
                   4590:         return $response;
1.521     raeburn  4591:     }
                   4592: }
                   4593: 
1.792     raeburn  4594: sub auto_instcode_defaults {
                   4595:     my ($domain,$returnhash,$code_order) = @_;
                   4596:     my @homeservers;
1.841     albertel 4597: 
                   4598:     my %servers = &get_servers($domain,'library');
                   4599:     foreach my $tryserver (keys(%servers)) {
                   4600: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   4601: 	    push(@homeservers,$tryserver);
                   4602: 	}
1.792     raeburn  4603:     }
1.841     albertel 4604: 
1.792     raeburn  4605:     my $response;
1.841     albertel 4606:     foreach my $server (@homeservers) {
1.792     raeburn  4607:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 4608:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   4609: 	
                   4610: 	foreach my $pair (split(/\&/,$response)) {
                   4611: 	    my ($name,$value)=split(/\=/,$pair);
                   4612: 	    if ($name eq 'code_order') {
                   4613: 		@{$code_order} = split(/\&/,&unescape($value));
                   4614: 	    } else {
                   4615: 		$returnhash->{&unescape($name)}=&unescape($value);
                   4616: 	    }
                   4617: 	}
                   4618: 	return 'ok';
1.792     raeburn  4619:     }
1.841     albertel 4620: 
                   4621:     return $response;
1.792     raeburn  4622: } 
                   4623: 
1.777     albertel 4624: sub auto_validate_class_sec {
1.773     raeburn  4625:     my ($cdom,$cnum,$owner,$inst_class) = @_;
                   4626:     my $homeserver = &homeserver($cnum,$cdom);
                   4627:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.774     banghart 4628:                         &escape($owner).':'.$cdom,$homeserver);
1.773     raeburn  4629:     return $response;
                   4630: }
                   4631: 
1.679     raeburn  4632: # ------------------------------------------------------- Course Group routines
                   4633: 
                   4634: sub get_coursegroups {
1.809     raeburn  4635:     my ($cdom,$cnum,$group,$namespace) = @_;
                   4636:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  4637: }
                   4638: 
1.679     raeburn  4639: sub modify_coursegroup {
                   4640:     my ($cdom,$cnum,$groupsettings) = @_;
                   4641:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   4642: }
                   4643: 
1.809     raeburn  4644: sub toggle_coursegroup_status {
                   4645:     my ($cdom,$cnum,$group,$action) = @_;
                   4646:     my ($from_namespace,$to_namespace);
                   4647:     if ($action eq 'delete') {
                   4648:         $from_namespace = 'coursegroups';
                   4649:         $to_namespace = 'deleted_groups';
                   4650:     } else {
                   4651:         $from_namespace = 'deleted_groups';
                   4652:         $to_namespace = 'coursegroups';
                   4653:     }
                   4654:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  4655:     if (my $tmp = &error(%curr_group)) {
                   4656:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   4657:         return ('read error',$tmp);
                   4658:     } else {
                   4659:         my %savedsettings = %curr_group; 
1.809     raeburn  4660:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  4661:         my $deloutcome;
                   4662:         if ($result eq 'ok') {
1.809     raeburn  4663:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  4664:         } else {
                   4665:             return ('write error',$result);
                   4666:         }
                   4667:         if ($deloutcome eq 'ok') {
                   4668:             return 'ok';
                   4669:         } else {
                   4670:             return ('delete error',$deloutcome);
                   4671:         }
                   4672:     }
                   4673: }
                   4674: 
1.679     raeburn  4675: sub modify_group_roles {
                   4676:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs) = @_;
                   4677:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   4678:     my $role = 'gr/'.&escape($userprivs);
                   4679:     my ($uname,$udom) = split(/:/,$user);
                   4680:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start);
1.684     raeburn  4681:     if ($result eq 'ok') {
                   4682:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   4683:     }
1.679     raeburn  4684:     return $result;
                   4685: }
                   4686: 
                   4687: sub modify_coursegroup_membership {
                   4688:     my ($cdom,$cnum,$membership) = @_;
                   4689:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   4690:     return $result;
                   4691: }
                   4692: 
1.682     raeburn  4693: sub get_active_groups {
                   4694:     my ($udom,$uname,$cdom,$cnum) = @_;
                   4695:     my $now = time;
                   4696:     my %groups = ();
                   4697:     foreach my $key (keys(%env)) {
1.811     albertel 4698:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  4699:             my ($start,$end) = split(/\./,$env{$key});
                   4700:             if (($end!=0) && ($end<$now)) { next; }
                   4701:             if (($start!=0) && ($start>$now)) { next; }
                   4702:             if ($1 eq $cdom && $2 eq $cnum) {
                   4703:                 $groups{$3} = $env{$key} ;
                   4704:             }
                   4705:         }
                   4706:     }
                   4707:     return %groups;
                   4708: }
                   4709: 
1.683     raeburn  4710: sub get_group_membership {
                   4711:     my ($cdom,$cnum,$group) = @_;
                   4712:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   4713: }
                   4714: 
                   4715: sub get_users_groups {
                   4716:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  4717:     my @usersgroups;
1.683     raeburn  4718:     my $cachetime=1800;
                   4719: 
                   4720:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  4721:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   4722:     if (defined($cached)) {
1.734     albertel 4723:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  4724:     } else {  
                   4725:         $grouplist = '';
1.816     raeburn  4726:         my $courseurl = &courseid_to_courseurl($courseid);
                   4727:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817     raeburn  4728:         my $access_end = $env{'course.'.$courseid.
                   4729:                               '.default_enrollment_end_date'};
                   4730:         my $now = time;
                   4731:         foreach my $key (keys(%roleshash)) {
                   4732:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   4733:                 my $group = $1;
                   4734:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   4735:                     my $start = $2;
                   4736:                     my $end = $1;
                   4737:                     if ($start == -1) { next; } # deleted from group
                   4738:                     if (($start!=0) && ($start>$now)) { next; }
                   4739:                     if (($end!=0) && ($end<$now)) {
                   4740:                         if ($access_end && $access_end < $now) {
                   4741:                             if ($access_end - $end < 86400) {
                   4742:                                 push(@usersgroups,$group);
1.733     raeburn  4743:                             }
                   4744:                         }
1.817     raeburn  4745:                         next;
1.733     raeburn  4746:                     }
1.817     raeburn  4747:                     push(@usersgroups,$group);
1.683     raeburn  4748:                 }
                   4749:             }
                   4750:         }
1.817     raeburn  4751:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   4752:         $grouplist = join(':',@usersgroups);
                   4753:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  4754:     }
1.733     raeburn  4755:     return @usersgroups;
1.683     raeburn  4756: }
                   4757: 
                   4758: sub devalidate_getgroups_cache {
                   4759:     my ($udom,$uname,$cdom,$cnum)=@_;
                   4760:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 4761: 
1.683     raeburn  4762:     my $hashid="$udom:$uname:$courseid";
                   4763:     &devalidate_cache_new('getgroups',$hashid);
                   4764: }
                   4765: 
1.12      www      4766: # ------------------------------------------------------------------ Plain Text
                   4767: 
                   4768: sub plaintext {
1.742     raeburn  4769:     my ($short,$type,$cid) = @_;
1.758     albertel 4770:     if ($short =~ /^cr/) {
                   4771: 	return (split('/',$short))[-1];
                   4772:     }
1.742     raeburn  4773:     if (!defined($cid)) {
                   4774:         $cid = $env{'request.course.id'};
                   4775:     }
                   4776:     if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) {
                   4777:         return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short.
                   4778:                                           '.plaintext'});
                   4779:     }
                   4780:     my %rolenames = (
                   4781:                       Course => 'std',
                   4782:                       Group => 'alt1',
                   4783:                     );
                   4784:     if (defined($type) && 
                   4785:          defined($rolenames{$type}) && 
                   4786:          defined($prp{$short}{$rolenames{$type}})) {
                   4787:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
                   4788:     } else {
                   4789:         return &Apache::lonlocal::mt($prp{$short}{'std'});
                   4790:     }
1.12      www      4791: }
                   4792: 
                   4793: # ----------------------------------------------------------------- Assign Role
                   4794: 
                   4795: sub assignrole {
1.357     www      4796:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
1.21      www      4797:     my $mrole;
                   4798:     if ($role =~ /^cr\//) {
1.393     www      4799:         my $cwosec=$url;
1.811     albertel 4800:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      4801: 	unless (&allowed('ccr',$cwosec)) {
1.104     www      4802:            &logthis('Refused custom assignrole: '.
                   4803:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620     albertel 4804: 		    $env{'user.name'}.' at '.$env{'user.domain'});
1.104     www      4805:            return 'refused'; 
                   4806:         }
1.21      www      4807:         $mrole='cr';
1.678     raeburn  4808:     } elsif ($role =~ /^gr\//) {
                   4809:         my $cwogrp=$url;
1.811     albertel 4810:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  4811:         unless (&allowed('mdg',$cwogrp)) {
                   4812:             &logthis('Refused group assignrole: '.
                   4813:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   4814:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   4815:             return 'refused';
                   4816:         }
                   4817:         $mrole='gr';
1.21      www      4818:     } else {
1.82      www      4819:         my $cwosec=$url;
1.811     albertel 4820:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.373     www      4821:         unless ((&allowed('c'.$role,$cwosec)) || &allowed('c'.$role,$udom)) { 
1.104     www      4822:            &logthis('Refused assignrole: '.
                   4823:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620     albertel 4824: 		    $env{'user.name'}.' at '.$env{'user.domain'});
1.104     www      4825:            return 'refused'; 
                   4826:         }
1.21      www      4827:         $mrole=$role;
                   4828:     }
1.620     albertel 4829:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      4830:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      4831:     if ($end) { $command.='_'.$end; }
1.21      www      4832:     if ($start) {
                   4833: 	if ($end) { 
1.81      www      4834:            $command.='_'.$start; 
1.21      www      4835:         } else {
1.81      www      4836:            $command.='_0_'.$start;
1.21      www      4837:         }
                   4838:     }
1.739     raeburn  4839:     my $origstart = $start;
                   4840:     my $origend = $end;
1.357     www      4841: # actually delete
                   4842:     if ($deleteflag) {
1.373     www      4843: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      4844: # modify command to delete the role
1.620     albertel 4845:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      4846:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 4847: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      4848: # set start and finish to negative values for userrolelog
                   4849:            $start=-1;
                   4850:            $end=-1;
                   4851:         }
                   4852:     }
                   4853: # send command
1.349     www      4854:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      4855: # log new user role if status is ok
1.349     www      4856:     if ($answer eq 'ok') {
1.663     raeburn  4857: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.739     raeburn  4858: # for course roles, perform group memberships changes triggered by role change.
                   4859:         unless ($role =~ /^gr/) {
                   4860:             &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
                   4861:                                              $origstart);
                   4862:         }
1.349     www      4863:     }
                   4864:     return $answer;
1.169     harris41 4865: }
                   4866: 
                   4867: # -------------------------------------------------- Modify user authentication
1.197     www      4868: # Overrides without validation
                   4869: 
1.169     harris41 4870: sub modifyuserauth {
                   4871:     my ($udom,$uname,$umode,$upass)=@_;
                   4872:     my $uhome=&homeserver($uname,$udom);
1.197     www      4873:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   4874:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 4875:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   4876:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 4877:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   4878: 		     &escape($upass),$uhome);
1.620     albertel 4879:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      4880:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   4881:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   4882:     &log($udom,,$uname,$uhome,
1.620     albertel 4883:         'Authentication changed by '.$env{'user.domain'}.', '.
                   4884:                                      $env{'user.name'}.', '.$umode.
1.197     www      4885:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 4886:     unless ($reply eq 'ok') {
1.197     www      4887:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 4888: 	return 'error: '.$reply;
                   4889:     }   
1.170     harris41 4890:     return 'ok';
1.80      www      4891: }
                   4892: 
1.81      www      4893: # --------------------------------------------------------------- Modify a user
1.80      www      4894: 
1.81      www      4895: sub modifyuser {
1.206     matthew  4896:     my ($udom,    $uname, $uid,
                   4897:         $umode,   $upass, $first,
                   4898:         $middle,  $last,  $gene,
1.387     www      4899:         $forceid, $desiredhome, $email)=@_;
1.807     albertel 4900:     $udom= &LONCAPA::clean_domain($udom);
                   4901:     $uname=&LONCAPA::clean_username($uname);
1.81      www      4902:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      4903:              $umode.', '.$first.', '.$middle.', '.
1.206     matthew  4904: 	     $last.', '.$gene.'(forceid: '.$forceid.')'.
                   4905:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   4906:                                      ' desiredhome not specified'). 
1.620     albertel 4907:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   4908:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 4909:     my $uhome=&homeserver($uname,$udom,'true');
1.80      www      4910: # ----------------------------------------------------------------- Create User
1.406     albertel 4911:     if (($uhome eq 'no_host') && 
                   4912: 	(($umode && $upass) || ($umode eq 'localauth'))) {
1.80      www      4913:         my $unhome='';
1.844     albertel 4914:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  4915:             $unhome = $desiredhome;
1.620     albertel 4916: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   4917: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  4918:         } else { # load balancing routine for determining $unhome
1.81      www      4919:             my $loadm=10000000;
1.841     albertel 4920: 	    my %servers = &get_servers($udom,'library');
                   4921: 	    foreach my $tryserver (keys(%servers)) {
                   4922: 		my $answer=reply('load',$tryserver);
                   4923: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   4924: 		    $loadm=$answer;
                   4925: 		    $unhome=$tryserver;
                   4926: 		}
1.80      www      4927: 	    }
                   4928:         }
                   4929:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  4930: 	    return 'error: unable to find a home server for '.$uname.
                   4931:                    ' in domain '.$udom;
1.80      www      4932:         }
                   4933:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   4934:                          &escape($upass),$unhome);
                   4935: 	unless ($reply eq 'ok') {
                   4936:             return 'error: '.$reply;
                   4937:         }   
1.230     stredwic 4938:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      4939:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  4940: 	    return 'error: unable verify users home machine.';
1.80      www      4941:         }
1.209     matthew  4942:     }   # End of creation of new user
1.80      www      4943: # ---------------------------------------------------------------------- Add ID
                   4944:     if ($uid) {
                   4945:        $uid=~tr/A-Z/a-z/;
                   4946:        my %uidhash=&idrget($udom,$uname);
1.196     www      4947:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   4948:          && (!$forceid)) {
1.80      www      4949: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  4950: 	      return 'error: user id "'.$uid.'" does not match '.
                   4951:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      4952:           }
                   4953:        } else {
                   4954: 	  &idput($udom,($uname => $uid));
                   4955:        }
                   4956:     }
                   4957: # -------------------------------------------------------------- Add names, etc
1.313     matthew  4958:     my @tmp=&get('environment',
1.134     albertel 4959: 		   ['firstname','middlename','lastname','generation'],
                   4960: 		   $udom,$uname);
1.313     matthew  4961:     my %names;
                   4962:     if ($tmp[0] =~ m/^error:.*/) { 
                   4963:         %names=(); 
                   4964:     } else {
                   4965:         %names = @tmp;
                   4966:     }
1.388     www      4967: #
                   4968: # Make sure to not trash student environment if instructor does not bother
                   4969: # to supply name and email information
                   4970: #
                   4971:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  4972:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      4973:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  4974:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      4975:     if ($email) {
                   4976:        $email=~s/[^\w\@\.\-\,]//gs;
                   4977:        if ($email=~/\@/) { $names{'notification'} = $email;
                   4978: 			   $names{'critnotification'} = $email;
                   4979: 			   $names{'permanentemail'} = $email; }
                   4980:     }
1.134     albertel 4981:     my $reply = &put('environment', \%names, $udom,$uname);
                   4982:     if ($reply ne 'ok') { return 'error: '.$reply; }
1.680     www      4983:     &devalidate_cache_new('namescache',$uname.':'.$udom);
1.81      www      4984:     &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      4985:              $umode.', '.$first.', '.$middle.', '.
                   4986: 	     $last.', '.$gene.' by '.
1.620     albertel 4987:              $env{'user.name'}.' at '.$env{'user.domain'});
1.134     albertel 4988:     return 'ok';
1.80      www      4989: }
                   4990: 
1.81      www      4991: # -------------------------------------------------------------- Modify student
1.80      www      4992: 
1.81      www      4993: sub modifystudent {
                   4994:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.515     raeburn  4995:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
1.455     albertel 4996:     if (!$cid) {
1.620     albertel 4997: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 4998: 	    return 'not_in_class';
                   4999: 	}
1.80      www      5000:     }
                   5001: # --------------------------------------------------------------- Make the user
1.81      www      5002:     my $reply=&modifyuser
1.209     matthew  5003: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.387     www      5004:          $desiredhome,$email);
1.80      www      5005:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  5006:     # This will cause &modify_student_enrollment to get the uid from the
                   5007:     # students environment
                   5008:     $uid = undef if (!$forceid);
1.455     albertel 5009:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.515     raeburn  5010: 					$gene,$usec,$end,$start,$type,$locktype,$cid);
1.297     matthew  5011:     return $reply;
                   5012: }
                   5013: 
                   5014: sub modify_student_enrollment {
1.515     raeburn  5015:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
1.455     albertel 5016:     my ($cdom,$cnum,$chome);
                   5017:     if (!$cid) {
1.620     albertel 5018: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 5019: 	    return 'not_in_class';
                   5020: 	}
1.620     albertel 5021: 	$cdom=$env{'course.'.$cid.'.domain'};
                   5022: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 5023:     } else {
                   5024: 	($cdom,$cnum)=split(/_/,$cid);
                   5025:     }
1.620     albertel 5026:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 5027:     if (!$chome) {
1.457     raeburn  5028: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  5029:     }
1.455     albertel 5030:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  5031:     # Make sure the user exists
1.81      www      5032:     my $uhome=&homeserver($uname,$udom);
                   5033:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   5034: 	return 'error: no such user';
                   5035:     }
1.297     matthew  5036:     # Get student data if we were not given enough information
                   5037:     if (!defined($first)  || $first  eq '' || 
                   5038:         !defined($last)   || $last   eq '' || 
                   5039:         !defined($uid)    || $uid    eq '' || 
                   5040:         !defined($middle) || $middle eq '' || 
                   5041:         !defined($gene)   || $gene   eq '') {
1.294     matthew  5042:         # They did not supply us with enough data to enroll the student, so
                   5043:         # we need to pick up more information.
1.297     matthew  5044:         my %tmp = &get('environment',
1.294     matthew  5045:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  5046:                        ,$udom,$uname);
                   5047: 
1.800     albertel 5048:         #foreach my $key (keys(%tmp)) {
                   5049:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 5050:         #}
1.294     matthew  5051:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   5052:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   5053:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  5054:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  5055:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   5056:     }
1.556     albertel 5057:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487     albertel 5058:     my $reply=cput('classlist',
                   5059: 		   {"$uname:$udom" => 
1.515     raeburn  5060: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487     albertel 5061: 		   $cdom,$cnum);
1.81      www      5062:     unless (($reply eq 'ok') || ($reply eq 'delayed')) {
                   5063: 	return 'error: '.$reply;
1.652     albertel 5064:     } else {
                   5065: 	&devalidate_getsection_cache($udom,$uname,$cid);
1.81      www      5066:     }
1.297     matthew  5067:     # Add student role to user
1.83      www      5068:     my $uurl='/'.$cid;
1.81      www      5069:     $uurl=~s/\_/\//g;
                   5070:     if ($usec) {
                   5071: 	$uurl.='/'.$usec;
                   5072:     }
                   5073:     return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1.21      www      5074: }
                   5075: 
1.556     albertel 5076: sub format_name {
                   5077:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   5078:     my $name;
                   5079:     if ($first ne 'lastname') {
                   5080: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   5081:     } else {
                   5082: 	if ($lastname=~/\S/) {
                   5083: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   5084: 	    $name=~s/\s+,/,/;
                   5085: 	} else {
                   5086: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   5087: 	}
                   5088:     }
                   5089:     $name=~s/^\s+//;
                   5090:     $name=~s/\s+$//;
                   5091:     $name=~s/\s+/ /g;
                   5092:     return $name;
                   5093: }
                   5094: 
1.84      www      5095: # ------------------------------------------------- Write to course preferences
                   5096: 
                   5097: sub writecoursepref {
                   5098:     my ($courseid,%prefs)=@_;
                   5099:     $courseid=~s/^\///;
                   5100:     $courseid=~s/\_/\//g;
                   5101:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   5102:     my $chome=homeserver($cnum,$cdomain);
                   5103:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   5104: 	return 'error: no such course';
                   5105:     }
                   5106:     my $cstring='';
1.800     albertel 5107:     foreach my $pref (keys(%prefs)) {
                   5108: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 5109:     }
1.84      www      5110:     $cstring=~s/\&$//;
                   5111:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   5112: }
                   5113: 
                   5114: # ---------------------------------------------------------- Make/modify course
                   5115: 
                   5116: sub createcourse {
1.741     raeburn  5117:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
                   5118:         $course_owner,$crstype)=@_;
1.84      www      5119:     $url=&declutter($url);
                   5120:     my $cid='';
1.264     matthew  5121:     unless (&allowed('ccc',$udom)) {
1.84      www      5122:         return 'refused';
                   5123:     }
                   5124: # ------------------------------------------------------------------- Create ID
1.674     www      5125:    my $uname=int(1+rand(9)).
                   5126:        ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   5127:        substr($$.time,0,5).unpack("H8",pack("I32",time)).
1.84      www      5128:        unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   5129: # ----------------------------------------------- Make sure that does not exist
1.230     stredwic 5130:    my $uhome=&homeserver($uname,$udom,'true');
1.84      www      5131:    unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   5132:        $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   5133:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1.230     stredwic 5134:        $uhome=&homeserver($uname,$udom,'true');       
1.84      www      5135:        unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   5136:            return 'error: unable to generate unique course-ID';
                   5137:        } 
                   5138:    }
1.264     matthew  5139: # ------------------------------------------------ Check supplied server name
1.620     albertel 5140:     $course_server = $env{'user.homeserver'} if (! defined($course_server));
1.845     albertel 5141:     if (! &is_library($course_server)) {
1.264     matthew  5142:         return 'error:bad server name '.$course_server;
                   5143:     }
1.84      www      5144: # ------------------------------------------------------------- Make the course
                   5145:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  5146:                       $course_server);
1.84      www      5147:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.230     stredwic 5148:     $uhome=&homeserver($uname,$udom,'true');
1.84      www      5149:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   5150: 	return 'error: no such course';
                   5151:     }
1.271     www      5152: # ----------------------------------------------------------------- Course made
1.516     raeburn  5153: # log existence
                   5154:     &courseidput($udom,&escape($udom.'_'.$uname).'='.&escape($description).
1.741     raeburn  5155:                  ':'.&escape($inst_code).':'.&escape($course_owner).':'.
                   5156:                   &escape($crstype),$uhome);
1.358     www      5157:     &flushcourselogs();
                   5158: # set toplevel url
1.271     www      5159:     my $topurl=$url;
                   5160:     unless ($nonstandard) {
                   5161: # ------------------------------------------ For standard courses, make top url
                   5162:         my $mapurl=&clutter($url);
1.278     www      5163:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 5164:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      5165: <map>
                   5166: <resource id="1" type="start"></resource>
                   5167: <resource id="2" src="$mapurl"></resource>
                   5168: <resource id="3" type="finish"></resource>
                   5169: <link index="1" from="1" to="2"></link>
                   5170: <link index="2" from="2" to="3"></link>
                   5171: </map>
                   5172: ENDINITMAP
                   5173:         $topurl=&declutter(
1.638     albertel 5174:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      5175:                           );
                   5176:     }
                   5177: # ----------------------------------------------------------- Write preferences
1.84      www      5178:     &writecoursepref($udom.'_'.$uname,
                   5179:                      ('description' => $description,
1.271     www      5180:                       'url'         => $topurl));
1.84      www      5181:     return '/'.$udom.'/'.$uname;
                   5182: }
                   5183: 
1.813     albertel 5184: sub is_course {
                   5185:     my ($cdom,$cnum) = @_;
                   5186:     my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
                   5187: 				undef,'.');
                   5188:     if (exists($courses{$cdom.'_'.$cnum})) {
                   5189:         return 1;
                   5190:     }
                   5191:     return 0;
                   5192: }
                   5193: 
1.21      www      5194: # ---------------------------------------------------------- Assign Custom Role
                   5195: 
                   5196: sub assigncustomrole {
1.357     www      5197:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
1.21      www      5198:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.357     www      5199:                        $end,$start,$deleteflag);
1.21      www      5200: }
                   5201: 
                   5202: # ----------------------------------------------------------------- Revoke Role
                   5203: 
                   5204: sub revokerole {
1.357     www      5205:     my ($udom,$uname,$url,$role,$deleteflag)=@_;
1.21      www      5206:     my $now=time;
1.357     www      5207:     return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
1.21      www      5208: }
                   5209: 
                   5210: # ---------------------------------------------------------- Revoke Custom Role
                   5211: 
                   5212: sub revokecustomrole {
1.357     www      5213:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
1.21      www      5214:     my $now=time;
1.357     www      5215:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
                   5216:            $deleteflag);
1.17      www      5217: }
                   5218: 
1.533     banghart 5219: # ------------------------------------------------------------ Disk usage
1.535     albertel 5220: sub diskusage {
1.533     banghart 5221:     my ($udom,$uname,$directoryRoot)=@_;
                   5222:     $directoryRoot =~ s/\/$//;
1.535     albertel 5223:     my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
1.514     albertel 5224:     return $listing;
1.512     banghart 5225: }
                   5226: 
1.566     banghart 5227: sub is_locked {
                   5228:     my ($file_name, $domain, $user) = @_;
                   5229:     my @check;
                   5230:     my $is_locked;
                   5231:     push @check, $file_name;
1.613     albertel 5232:     my %locked = &get('file_permissions',\@check,
1.620     albertel 5233: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 5234:     my ($tmp)=keys(%locked);
                   5235:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  5236:     
1.566     banghart 5237:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  5238:         $is_locked = 'false';
                   5239:         foreach my $entry (@{$locked{$file_name}}) {
                   5240:            if (ref($entry) eq 'ARRAY') { 
1.746     raeburn  5241:                $is_locked = 'true';
                   5242:                last;
1.745     raeburn  5243:            }
                   5244:        }
1.566     banghart 5245:     } else {
                   5246:         $is_locked = 'false';
                   5247:     }
                   5248: }
                   5249: 
1.759     albertel 5250: sub declutter_portfile {
                   5251:     my ($file) = @_;
1.833     albertel 5252:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 5253:     return $file;
                   5254: }
                   5255: 
1.559     banghart 5256: # ------------------------------------------------------------- Mark as Read Only
                   5257: 
                   5258: sub mark_as_readonly {
                   5259:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 5260:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 5261:     my ($tmp)=keys(%current_permissions);
                   5262:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 5263:     foreach my $file (@{$files}) {
1.759     albertel 5264: 	$file = &declutter_portfile($file);
1.561     banghart 5265:         push(@{$current_permissions{$file}},$what);
1.559     banghart 5266:     }
1.613     albertel 5267:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 5268:     return;
                   5269: }
                   5270: 
1.572     banghart 5271: # ------------------------------------------------------------Save Selected Files
                   5272: 
                   5273: sub save_selected_files {
                   5274:     my ($user, $path, @files) = @_;
                   5275:     my $filename = $user."savedfiles";
1.573     banghart 5276:     my @other_files = &files_not_in_path($user, $path);
1.574     banghart 5277:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573     banghart 5278:     foreach my $file (@files) {
1.620     albertel 5279:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 5280:     }
                   5281:     foreach my $file (@other_files) {
1.574     banghart 5282:         print (OUT $file."\n");
1.572     banghart 5283:     }
1.574     banghart 5284:     close (OUT);
1.572     banghart 5285:     return 'ok';
                   5286: }
                   5287: 
1.574     banghart 5288: sub clear_selected_files {
                   5289:     my ($user) = @_;
                   5290:     my $filename = $user."savedfiles";
                   5291:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   5292:     print (OUT undef);
                   5293:     close (OUT);
                   5294:     return ("ok");    
                   5295: }
                   5296: 
1.572     banghart 5297: sub files_in_path {
                   5298:     my ($user, $path) = @_;
                   5299:     my $filename = $user."savedfiles";
                   5300:     my %return_files;
1.574     banghart 5301:     open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573     banghart 5302:     while (my $line_in = <IN>) {
1.574     banghart 5303:         chomp ($line_in);
                   5304:         my @paths_and_file = split (m!/!, $line_in);
                   5305:         my $file_part = pop (@paths_and_file);
                   5306:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 5307:         $path_part.='/';
                   5308:         my $path_and_file = $path_part.$file_part;
                   5309:         if ($path_part eq $path) {
                   5310:             $return_files{$file_part}= 'selected';
                   5311:         }
                   5312:     }
1.574     banghart 5313:     close (IN);
                   5314:     return (\%return_files);
1.572     banghart 5315: }
                   5316: 
                   5317: # called in portfolio select mode, to show files selected NOT in current directory
                   5318: sub files_not_in_path {
                   5319:     my ($user, $path) = @_;
                   5320:     my $filename = $user."savedfiles";
                   5321:     my @return_files;
                   5322:     my $path_part;
1.800     albertel 5323:     open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   5324:     while (my $line = <IN>) {
1.572     banghart 5325:         #ok, I know it's clunky, but I want it to work
1.800     albertel 5326:         my @paths_and_file = split(m|/|, $line);
                   5327:         my $file_part = pop(@paths_and_file);
                   5328:         chomp($file_part);
                   5329:         my $path_part = join('/', @paths_and_file);
1.572     banghart 5330:         $path_part .= '/';
                   5331:         my $path_and_file = $path_part.$file_part;
                   5332:         if ($path_part ne $path) {
1.800     albertel 5333:             push(@return_files, ($path_and_file));
1.572     banghart 5334:         }
                   5335:     }
1.800     albertel 5336:     close(OUT);
1.574     banghart 5337:     return (@return_files);
1.572     banghart 5338: }
                   5339: 
1.745     raeburn  5340: #----------------------------------------------Get portfolio file permissions
1.629     banghart 5341: 
1.745     raeburn  5342: sub get_portfile_permissions {
                   5343:     my ($domain,$user) = @_;
1.613     albertel 5344:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 5345:     my ($tmp)=keys(%current_permissions);
                   5346:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  5347:     return \%current_permissions;
                   5348: }
                   5349: 
                   5350: #---------------------------------------------Get portfolio file access controls
                   5351: 
1.749     raeburn  5352: sub get_access_controls {
1.745     raeburn  5353:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 5354:     my %access;
                   5355:     my $real_file = $file;
                   5356:     $file =~ s/\.meta$//;
1.745     raeburn  5357:     if (defined($file)) {
1.749     raeburn  5358:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   5359:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 5360:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  5361:             }
                   5362:         }
1.745     raeburn  5363:     } else {
1.749     raeburn  5364:         foreach my $key (keys(%{$current_permissions})) {
                   5365:             if ($key =~ /\0accesscontrol$/) {
                   5366:                 if (defined($group)) {
                   5367:                     if ($key !~ m-^\Q$group\E/-) {
                   5368:                         next;
                   5369:                     }
                   5370:                 }
                   5371:                 my ($fullpath) = split(/\0/,$key);
                   5372:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   5373:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   5374:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   5375:                     }
                   5376:                 }
                   5377:             }
                   5378:         }
                   5379:     }
                   5380:     return %access;
                   5381: }
                   5382: 
                   5383: sub modify_access_controls {
                   5384:     my ($file_name,$changes,$domain,$user)=@_;
                   5385:     my ($outcome,$deloutcome);
                   5386:     my %store_permissions;
                   5387:     my %new_values;
                   5388:     my %new_control;
                   5389:     my %translation;
                   5390:     my @deletions = ();
                   5391:     my $now = time;
                   5392:     if (exists($$changes{'activate'})) {
                   5393:         if (ref($$changes{'activate'}) eq 'HASH') {
                   5394:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   5395:             my $numnew = scalar(@newitems);
                   5396:             for (my $i=0; $i<$numnew; $i++) {
                   5397:                 my $newkey = $newitems[$i];
                   5398:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  5399:                 if ($newkey =~ /^\d+:/) { 
                   5400:                     $newkey =~ s/^(\d+)/$newid/;
                   5401:                     $translation{$1} = $newid;
                   5402:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   5403:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   5404:                     $translation{$1} = $newid;
                   5405:                 }
1.749     raeburn  5406:                 $new_values{$file_name."\0".$newkey} = 
                   5407:                                           $$changes{'activate'}{$newitems[$i]};
                   5408:                 $new_control{$newkey} = $now;
                   5409:             }
                   5410:         }
                   5411:     }
                   5412:     my %todelete;
                   5413:     my %changed_items;
                   5414:     foreach my $action ('delete','update') {
                   5415:         if (exists($$changes{$action})) {
                   5416:             if (ref($$changes{$action}) eq 'HASH') {
                   5417:                 foreach my $key (keys(%{$$changes{$action}})) {
                   5418:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   5419:                     if ($action eq 'delete') { 
                   5420:                         $todelete{$itemnum} = 1;
                   5421:                     } else {
                   5422:                         $changed_items{$itemnum} = $key;
                   5423:                     }
                   5424:                 }
1.745     raeburn  5425:             }
                   5426:         }
1.749     raeburn  5427:     }
                   5428:     # get lock on access controls for file.
                   5429:     my $lockhash = {
                   5430:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   5431:                                                        ':'.$env{'user.domain'},
                   5432:                    }; 
                   5433:     my $tries = 0;
                   5434:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   5435:    
                   5436:     while (($gotlock ne 'ok') && $tries <3) {
                   5437:         $tries ++;
                   5438:         sleep 1;
                   5439:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   5440:     }
                   5441:     if ($gotlock eq 'ok') {
                   5442:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   5443:         my ($tmp)=keys(%curr_permissions);
                   5444:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   5445:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   5446:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   5447:             if (ref($curr_controls) eq 'HASH') {
                   5448:                 foreach my $control_item (keys(%{$curr_controls})) {
                   5449:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   5450:                     if (defined($todelete{$itemnum})) {
                   5451:                         push(@deletions,$file_name."\0".$control_item);
                   5452:                     } else {
                   5453:                         if (defined($changed_items{$itemnum})) {
                   5454:                             $new_control{$changed_items{$itemnum}} = $now;
                   5455:                             push(@deletions,$file_name."\0".$control_item);
                   5456:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   5457:                         } else {
                   5458:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   5459:                         }
                   5460:                     }
1.745     raeburn  5461:                 }
                   5462:             }
                   5463:         }
1.749     raeburn  5464:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   5465:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   5466:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   5467:         #  remove lock
                   5468:         my @del_lock = ($file_name."\0".'locked_access_records');
                   5469:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  5470:         my ($file,$group);
                   5471:         if (&is_course($domain,$user)) {
                   5472:             ($group,$file) = split(/\//,$file_name,2);
                   5473:         } else {
                   5474:             $file = $file_name;
                   5475:         }
                   5476:         my $sqlresult =
                   5477:             &update_portfolio_table($user,$domain,$file,'portfolio_access',
                   5478:                                     $group);
1.749     raeburn  5479:     } else {
                   5480:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  5481:     }
1.749     raeburn  5482:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  5483: }
                   5484: 
1.827     raeburn  5485: sub make_public_indefinitely {
                   5486:     my ($requrl) = @_;
                   5487:     my $now = time;
                   5488:     my $action = 'activate';
                   5489:     my $aclnum = 0;
                   5490:     if (&is_portfolio_url($requrl)) {
                   5491:         my (undef,$udom,$unum,$file_name,$group) =
                   5492:             &parse_portfolio_url($requrl);
                   5493:         my $current_perms = &get_portfile_permissions($udom,$unum);
                   5494:         my %access_controls = &get_access_controls($current_perms,
                   5495:                                                    $group,$file_name);
                   5496:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   5497:             my ($num,$scope,$end,$start) = 
                   5498:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   5499:             if ($scope eq 'public') {
                   5500:                 if ($start <= $now && $end == 0) {
                   5501:                     $action = 'none';
                   5502:                 } else {
                   5503:                     $action = 'update';
                   5504:                     $aclnum = $num;
                   5505:                 }
                   5506:                 last;
                   5507:             }
                   5508:         }
                   5509:         if ($action eq 'none') {
                   5510:              return 'ok';
                   5511:         } else {
                   5512:             my %changes;
                   5513:             my $newend = 0;
                   5514:             my $newstart = $now;
                   5515:             my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
                   5516:             $changes{$action}{$newkey} = {
                   5517:                 type => 'public',
                   5518:                 time => {
                   5519:                     start => $newstart,
                   5520:                     end   => $newend,
                   5521:                 },
                   5522:             };
                   5523:             my ($outcome,$deloutcome,$new_values,$translation) =
                   5524:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
                   5525:             return $outcome;
                   5526:         }
                   5527:     } else {
                   5528:         return 'invalid';
                   5529:     }
                   5530: }
                   5531: 
1.745     raeburn  5532: #------------------------------------------------------Get Marked as Read Only
                   5533: 
                   5534: sub get_marked_as_readonly {
                   5535:     my ($domain,$user,$what,$group) = @_;
                   5536:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 5537:     my @readonly_files;
1.629     banghart 5538:     my $cmp1=$what;
                   5539:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  5540:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   5541:         if (defined($group)) {
                   5542:             if ($file_name !~ m-^\Q$group\E/-) {
                   5543:                 next;
                   5544:             }
                   5545:         }
1.561     banghart 5546:         if (ref($value) eq "ARRAY"){
                   5547:             foreach my $stored_what (@{$value}) {
1.629     banghart 5548:                 my $cmp2=$stored_what;
1.759     albertel 5549:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  5550:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  5551:                 }
1.629     banghart 5552:                 if ($cmp1 eq $cmp2) {
1.561     banghart 5553:                     push(@readonly_files, $file_name);
1.745     raeburn  5554:                     last;
1.563     banghart 5555:                 } elsif (!defined($what)) {
                   5556:                     push(@readonly_files, $file_name);
1.745     raeburn  5557:                     last;
1.561     banghart 5558:                 }
                   5559:             }
1.745     raeburn  5560:         }
1.561     banghart 5561:     }
                   5562:     return @readonly_files;
                   5563: }
1.577     banghart 5564: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 5565: 
1.577     banghart 5566: sub get_marked_as_readonly_hash {
1.745     raeburn  5567:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 5568:     my %readonly_files;
1.745     raeburn  5569:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   5570:         if (defined($group)) {
                   5571:             if ($file_name !~ m-^\Q$group\E/-) {
                   5572:                 next;
                   5573:             }
                   5574:         }
1.577     banghart 5575:         if (ref($value) eq "ARRAY"){
                   5576:             foreach my $stored_what (@{$value}) {
1.745     raeburn  5577:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 5578:                     foreach my $lock_descriptor(@{$stored_what}) {
                   5579:                         if ($lock_descriptor eq 'graded') {
                   5580:                             $readonly_files{$file_name} = 'graded';
                   5581:                         } elsif ($lock_descriptor eq 'handback') {
                   5582:                             $readonly_files{$file_name} = 'handback';
                   5583:                         } else {
                   5584:                             if (!exists($readonly_files{$file_name})) {
                   5585:                                 $readonly_files{$file_name} = 'locked';
                   5586:                             }
                   5587:                         }
1.745     raeburn  5588:                     }
1.750     banghart 5589:                 } 
1.577     banghart 5590:             }
                   5591:         } 
                   5592:     }
                   5593:     return %readonly_files;
                   5594: }
1.559     banghart 5595: # ------------------------------------------------------------ Unmark as Read Only
                   5596: 
                   5597: sub unmark_as_readonly {
1.629     banghart 5598:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   5599:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  5600:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 5601:     $file_name = &declutter_portfile($file_name);
1.634     albertel 5602:     my $symb_crs = $what;
                   5603:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  5604:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 5605:     my ($tmp)=keys(%current_permissions);
                   5606:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  5607:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 5608:     foreach my $file (@readonly_files) {
1.759     albertel 5609: 	my $clean_file = &declutter_portfile($file);
                   5610: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 5611: 	my $current_locks = $current_permissions{$file};
1.563     banghart 5612:         my @new_locks;
                   5613:         my @del_keys;
                   5614:         if (ref($current_locks) eq "ARRAY"){
                   5615:             foreach my $locker (@{$current_locks}) {
1.632     albertel 5616:                 my $compare=$locker;
1.749     raeburn  5617:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  5618:                     $compare=join('',@{$locker});
1.746     raeburn  5619:                     if ($compare ne $symb_crs) {
                   5620:                         push(@new_locks, $locker);
                   5621:                     }
1.563     banghart 5622:                 }
                   5623:             }
1.650     albertel 5624:             if (scalar(@new_locks) > 0) {
1.563     banghart 5625:                 $current_permissions{$file} = \@new_locks;
                   5626:             } else {
                   5627:                 push(@del_keys, $file);
1.613     albertel 5628:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 5629:                 delete($current_permissions{$file});
1.563     banghart 5630:             }
                   5631:         }
1.561     banghart 5632:     }
1.613     albertel 5633:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 5634:     return;
                   5635: }
1.512     banghart 5636: 
1.17      www      5637: # ------------------------------------------------------------ Directory lister
                   5638: 
                   5639: sub dirlist {
1.253     stredwic 5640:     my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
                   5641: 
1.18      www      5642:     $uri=~s/^\///;
                   5643:     $uri=~s/\/$//;
1.253     stredwic 5644:     my ($udom, $uname);
                   5645:     (undef,$udom,$uname)=split(/\//,$uri);
                   5646:     if(defined($userdomain)) {
                   5647:         $udom = $userdomain;
                   5648:     }
                   5649:     if(defined($username)) {
                   5650:         $uname = $username;
                   5651:     }
                   5652: 
                   5653:     my $dirRoot = $perlvar{'lonDocRoot'};
                   5654:     if(defined($alternateDirectoryRoot)) {
                   5655:         $dirRoot = $alternateDirectoryRoot;
                   5656:         $dirRoot =~ s/\/$//;
1.751     banghart 5657:     }
1.253     stredwic 5658: 
                   5659:     if($udom) {
                   5660:         if($uname) {
1.800     albertel 5661:             my $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
                   5662: 				 &homeserver($uname,$udom));
1.605     matthew  5663:             my @listing_results;
                   5664:             if ($listing eq 'unknown_cmd') {
1.800     albertel 5665:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,
                   5666: 				  &homeserver($uname,$udom));
1.605     matthew  5667:                 @listing_results = split(/:/,$listing);
                   5668:             } else {
                   5669:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   5670:             }
                   5671:             return @listing_results;
1.253     stredwic 5672:         } elsif(!defined($alternateDirectoryRoot)) {
1.800     albertel 5673:             my %allusers;
1.841     albertel 5674: 	    my %servers = &get_servers($udom,'library');
                   5675: 	    foreach my $tryserver (keys(%servers)) {
                   5676: 		my $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   5677: 				     $udom, $tryserver);
                   5678: 		my @listing_results;
                   5679: 		if ($listing eq 'unknown_cmd') {
                   5680: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   5681: 				      $udom, $tryserver);
                   5682: 		    @listing_results = split(/:/,$listing);
                   5683: 		} else {
                   5684: 		    @listing_results =
                   5685: 			map { &unescape($_); } split(/:/,$listing);
                   5686: 		}
                   5687: 		if ($listing_results[0] ne 'no_such_dir' && 
                   5688: 		    $listing_results[0] ne 'empty'       &&
                   5689: 		    $listing_results[0] ne 'con_lost') {
                   5690: 		    foreach my $line (@listing_results) {
                   5691: 			my ($entry) = split(/&/,$line,2);
                   5692: 			$allusers{$entry} = 1;
                   5693: 		    }
                   5694: 		}
1.253     stredwic 5695:             }
                   5696:             my $alluserstr='';
1.800     albertel 5697:             foreach my $user (sort(keys(%allusers))) {
                   5698:                 $alluserstr.=$user.'&user:';
1.253     stredwic 5699:             }
                   5700:             $alluserstr=~s/:$//;
                   5701:             return split(/:/,$alluserstr);
                   5702:         } else {
1.800     albertel 5703:             return ('missing user name');
1.253     stredwic 5704:         }
                   5705:     } elsif(!defined($alternateDirectoryRoot)) {
1.841     albertel 5706:         my @all_domains = sort(&all_domains());
                   5707:          foreach my $domain (@all_domains) {
                   5708:              $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
                   5709:          }
                   5710:          return @all_domains;
                   5711:      } else {
1.800     albertel 5712:         return ('missing domain');
1.275     stredwic 5713:     }
                   5714: }
                   5715: 
                   5716: # --------------------------------------------- GetFileTimestamp
                   5717: # This function utilizes dirlist and returns the date stamp for
                   5718: # when it was last modified.  It will also return an error of -1
                   5719: # if an error occurs
                   5720: 
1.410     matthew  5721: ##
                   5722: ## FIXME: This subroutine assumes its caller knows something about the
                   5723: ## directory structure of the home server for the student ($root).
                   5724: ## Not a good assumption to make.  Since this is for looking up files
                   5725: ## in user directories, the full path should be constructed by lond, not
                   5726: ## whatever machine we request data from.
                   5727: ##
1.275     stredwic 5728: sub GetFileTimestamp {
                   5729:     my ($studentDomain,$studentName,$filename,$root)=@_;
1.807     albertel 5730:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   5731:     $studentName   = &LONCAPA::clean_username($studentName);
1.275     stredwic 5732:     my $subdir=$studentName.'__';
                   5733:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   5734:     my $proname="$studentDomain/$subdir/$studentName";
                   5735:     $proname .= '/'.$filename;
1.375     matthew  5736:     my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain, 
                   5737:                                               $studentName, $root);
1.275     stredwic 5738:     my @stats = split('&', $fileStat);
                   5739:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375     matthew  5740:         # @stats contains first the filename, then the stat output
                   5741:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 5742:     } else {
                   5743:         return -1;
1.253     stredwic 5744:     }
1.26      www      5745: }
                   5746: 
1.712     albertel 5747: sub stat_file {
                   5748:     my ($uri) = @_;
1.787     albertel 5749:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 5750: 
1.712     albertel 5751:     my ($udom,$uname,$file,$dir);
                   5752:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   5753: 	($udom,$uname,$file) =
1.811     albertel 5754: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 5755: 	$file = 'userfiles/'.$file;
1.740     www      5756: 	$dir = &propath($udom,$uname);
1.712     albertel 5757:     }
                   5758:     if ($uri =~ m-^/res/-) {
                   5759: 	($udom,$uname) = 
1.807     albertel 5760: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 5761: 	$file = $uri;
                   5762:     }
                   5763: 
                   5764:     if (!$udom || !$uname || !$file) {
                   5765: 	# unable to handle the uri
                   5766: 	return ();
                   5767:     }
                   5768: 
                   5769:     my ($result) = &dirlist($file,$udom,$uname,$dir);
                   5770:     my @stats = split('&', $result);
1.721     banghart 5771:     
1.712     albertel 5772:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
                   5773: 	shift(@stats); #filename is first
                   5774: 	return @stats;
                   5775:     }
                   5776:     return ();
                   5777: }
                   5778: 
1.26      www      5779: # -------------------------------------------------------- Value of a Condition
                   5780: 
1.713     albertel 5781: # gets the value of a specific preevaluated condition
                   5782: #    stored in the string  $env{user.state.<cid>}
                   5783: # or looks up a condition reference in the bighash and if if hasn't
                   5784: # already been evaluated recurses into docondval to get the value of
                   5785: # the condition, then memoizing it to 
                   5786: #   $env{user.state.<cid>.<condition>}
1.40      www      5787: sub directcondval {
                   5788:     my $number=shift;
1.620     albertel 5789:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 5790: 	&Apache::lonuserstate::evalstate();
                   5791:     }
1.713     albertel 5792:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   5793: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   5794:     } elsif ($number =~ /^_/) {
                   5795: 	my $sub_condition;
                   5796: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   5797: 		&GDBM_READER(),0640)) {
                   5798: 	    $sub_condition=$bighash{'conditions'.$number};
                   5799: 	    untie(%bighash);
                   5800: 	}
                   5801: 	my $value = &docondval($sub_condition);
                   5802: 	&appenv('user.state.'.$env{'request.course.id'}.".$number" => $value);
                   5803: 	return $value;
                   5804:     }
1.620     albertel 5805:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   5806:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      5807:     } else {
                   5808:        return 2;
                   5809:     }
                   5810: }
                   5811: 
1.713     albertel 5812: # get the collection of conditions for this resource
1.26      www      5813: sub condval {
                   5814:     my $condidx=shift;
1.54      www      5815:     my $allpathcond='';
1.713     albertel 5816:     foreach my $cond (split(/\|/,$condidx)) {
                   5817: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   5818: 	    $allpathcond.=
                   5819: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   5820: 	}
1.191     harris41 5821:     }
1.54      www      5822:     $allpathcond=~s/\|$//;
1.713     albertel 5823:     return &docondval($allpathcond);
                   5824: }
                   5825: 
                   5826: #evaluates an expression of conditions
                   5827: sub docondval {
                   5828:     my ($allpathcond) = @_;
                   5829:     my $result=0;
                   5830:     if ($env{'request.course.id'}
                   5831: 	&& defined($allpathcond)) {
                   5832: 	my $operand='|';
                   5833: 	my @stack;
                   5834: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   5835: 	    if ($chunk eq '(') {
                   5836: 		push @stack,($operand,$result);
                   5837: 	    } elsif ($chunk eq ')') {
                   5838: 		my $before=pop @stack;
                   5839: 		if (pop @stack eq '&') {
                   5840: 		    $result=$result>$before?$before:$result;
                   5841: 		} else {
                   5842: 		    $result=$result>$before?$result:$before;
                   5843: 		}
                   5844: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   5845: 		$operand=$chunk;
                   5846: 	    } else {
                   5847: 		my $new=directcondval($chunk);
                   5848: 		if ($operand eq '&') {
                   5849: 		    $result=$result>$new?$new:$result;
                   5850: 		} else {
                   5851: 		    $result=$result>$new?$result:$new;
                   5852: 		}
                   5853: 	    }
                   5854: 	}
1.26      www      5855:     }
                   5856:     return $result;
1.421     albertel 5857: }
                   5858: 
                   5859: # ---------------------------------------------------- Devalidate courseresdata
                   5860: 
                   5861: sub devalidatecourseresdata {
                   5862:     my ($coursenum,$coursedomain)=@_;
                   5863:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 5864:     &devalidate_cache_new('courseres',$hashid);
1.28      www      5865: }
                   5866: 
1.763     www      5867: 
1.200     www      5868: # --------------------------------------------------- Course Resourcedata Query
                   5869: 
1.624     albertel 5870: sub get_courseresdata {
                   5871:     my ($coursenum,$coursedomain)=@_;
1.200     www      5872:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   5873:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 5874:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 5875:     my %dumpreply;
1.417     albertel 5876:     unless (defined($cached)) {
1.624     albertel 5877: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 5878: 	$result=\%dumpreply;
1.251     albertel 5879: 	my ($tmp) = keys(%dumpreply);
                   5880: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 5881: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 5882: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   5883: 	    return $tmp;
1.416     albertel 5884: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 5885: 	    $result=undef;
1.599     albertel 5886: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 5887: 	}
                   5888:     }
1.624     albertel 5889:     return $result;
                   5890: }
                   5891: 
1.633     albertel 5892: sub devalidateuserresdata {
                   5893:     my ($uname,$udom)=@_;
                   5894:     my $hashid="$udom:$uname";
                   5895:     &devalidate_cache_new('userres',$hashid);
                   5896: }
                   5897: 
1.624     albertel 5898: sub get_userresdata {
                   5899:     my ($uname,$udom)=@_;
                   5900:     #most student don\'t have any data set, check if there is some data
                   5901:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   5902: 
                   5903:     my $hashid="$udom:$uname";
                   5904:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   5905:     if (!defined($cached)) {
                   5906: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   5907: 	$result=\%resourcedata;
                   5908: 	&do_cache_new('userres',$hashid,$result,600);
                   5909:     }
                   5910:     my ($tmp)=keys(%$result);
                   5911:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   5912: 	return $result;
                   5913:     }
                   5914:     #error 2 occurs when the .db doesn't exist
                   5915:     if ($tmp!~/error: 2 /) {
1.672     albertel 5916: 	&logthis("<font color=\"blue\">WARNING:".
1.624     albertel 5917: 		 " Trying to get resource data for ".
                   5918: 		 $uname." at ".$udom.": ".
                   5919: 		 $tmp."</font>");
                   5920:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 5921: 	#&EXT_cache_set($udom,$uname);
                   5922: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 5923: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 5924:     }
                   5925:     return $tmp;
                   5926: }
                   5927: 
                   5928: sub resdata {
                   5929:     my ($name,$domain,$type,@which)=@_;
                   5930:     my $result;
                   5931:     if ($type eq 'course') {
                   5932: 	$result=&get_courseresdata($name,$domain);
                   5933:     } elsif ($type eq 'user') {
                   5934: 	$result=&get_userresdata($name,$domain);
                   5935:     }
                   5936:     if (!ref($result)) { return $result; }    
1.251     albertel 5937:     foreach my $item (@which) {
1.417     albertel 5938: 	if (defined($result->{$item})) {
                   5939: 	    return $result->{$item};
1.251     albertel 5940: 	}
1.250     albertel 5941:     }
1.291     albertel 5942:     return undef;
1.200     www      5943: }
                   5944: 
1.379     matthew  5945: #
                   5946: # EXT resource caching routines
                   5947: #
                   5948: 
                   5949: sub clear_EXT_cache_status {
1.383     albertel 5950:     &delenv('cache.EXT.');
1.379     matthew  5951: }
                   5952: 
                   5953: sub EXT_cache_status {
                   5954:     my ($target_domain,$target_user) = @_;
1.383     albertel 5955:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 5956:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  5957:         # We know already the user has no data
                   5958:         return 1;
                   5959:     } else {
                   5960:         return 0;
                   5961:     }
                   5962: }
                   5963: 
                   5964: sub EXT_cache_set {
                   5965:     my ($target_domain,$target_user) = @_;
1.383     albertel 5966:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.633     albertel 5967:     #&appenv($cachename => time);
1.379     matthew  5968: }
                   5969: 
1.28      www      5970: # --------------------------------------------------------- Value of a Variable
1.58      www      5971: sub EXT {
1.715     albertel 5972: 
1.395     albertel 5973:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68      www      5974:     unless ($varname) { return ''; }
1.218     albertel 5975:     #get real user name/domain, courseid and symb
                   5976:     my $courseid;
1.359     albertel 5977:     my $publicuser;
1.427     www      5978:     if ($symbparm) {
                   5979: 	$symbparm=&get_symb_from_alias($symbparm);
                   5980:     }
1.218     albertel 5981:     if (!($uname && $udom)) {
1.790     albertel 5982:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 5983:       if (!$symbparm) {	$symbparm=$cursymb; }
                   5984:     } else {
1.620     albertel 5985: 	$courseid=$env{'request.course.id'};
1.218     albertel 5986:     }
1.48      www      5987:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   5988:     my $rest;
1.320     albertel 5989:     if (defined($therest[0])) {
1.48      www      5990:        $rest=join('.',@therest);
                   5991:     } else {
                   5992:        $rest='';
                   5993:     }
1.320     albertel 5994: 
1.57      www      5995:     my $qualifierrest=$qualifier;
                   5996:     if ($rest) { $qualifierrest.='.'.$rest; }
                   5997:     my $spacequalifierrest=$space;
                   5998:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      5999:     if ($realm eq 'user') {
1.48      www      6000: # --------------------------------------------------------------- user.resource
                   6001: 	if ($space eq 'resource') {
1.651     albertel 6002: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   6003: 		  || defined($Apache::lonhomework::parsing_a_task))
                   6004: 		 &&
1.744     albertel 6005: 		 ($symbparm eq &symbread()) ) {	
                   6006: 		# if we are in the middle of processing the resource the
                   6007: 		# get the value we are planning on committing
                   6008:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   6009:                     return $Apache::lonhomework::results{$qualifierrest};
                   6010:                 } else {
                   6011:                     return $Apache::lonhomework::history{$qualifierrest};
                   6012:                 }
1.335     albertel 6013: 	    } else {
1.359     albertel 6014: 		my %restored;
1.620     albertel 6015: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 6016: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   6017: 		} else {
                   6018: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   6019: 		}
1.335     albertel 6020: 		return $restored{$qualifierrest};
                   6021: 	    }
1.48      www      6022: # ----------------------------------------------------------------- user.access
                   6023:         } elsif ($space eq 'access') {
1.218     albertel 6024: 	    # FIXME - not supporting calls for a specific user
1.48      www      6025:             return &allowed($qualifier,$rest);
                   6026: # ------------------------------------------ user.preferences, user.environment
                   6027:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 6028: 	    if (($uname eq $env{'user.name'}) &&
                   6029: 		($udom eq $env{'user.domain'})) {
                   6030: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 6031: 	    } else {
1.359     albertel 6032: 		my %returnhash;
                   6033: 		if (!$publicuser) {
                   6034: 		    %returnhash=&userenvironment($udom,$uname,
                   6035: 						 $qualifierrest);
                   6036: 		}
1.218     albertel 6037: 		return $returnhash{$qualifierrest};
                   6038: 	    }
1.48      www      6039: # ----------------------------------------------------------------- user.course
                   6040:         } elsif ($space eq 'course') {
1.218     albertel 6041: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 6042:             return $env{join('.',('request.course',$qualifier))};
1.48      www      6043: # ------------------------------------------------------------------- user.role
                   6044:         } elsif ($space eq 'role') {
1.218     albertel 6045: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 6046:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      6047:             if ($qualifier eq 'value') {
                   6048: 		return $role;
                   6049:             } elsif ($qualifier eq 'extent') {
                   6050:                 return $where;
                   6051:             }
                   6052: # ----------------------------------------------------------------- user.domain
                   6053:         } elsif ($space eq 'domain') {
1.218     albertel 6054:             return $udom;
1.48      www      6055: # ------------------------------------------------------------------- user.name
                   6056:         } elsif ($space eq 'name') {
1.218     albertel 6057:             return $uname;
1.48      www      6058: # ---------------------------------------------------- Any other user namespace
1.29      www      6059:         } else {
1.359     albertel 6060: 	    my %reply;
                   6061: 	    if (!$publicuser) {
                   6062: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   6063: 	    }
                   6064: 	    return $reply{$qualifierrest};
1.48      www      6065:         }
1.236     www      6066:     } elsif ($realm eq 'query') {
                   6067: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 6068:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   6069: 						[$spacequalifierrest]);
1.620     albertel 6070: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      6071:    } elsif ($realm eq 'request') {
1.48      www      6072: # ------------------------------------------------------------- request.browser
                   6073:         if ($space eq 'browser') {
1.430     www      6074: 	    if ($qualifier eq 'textremote') {
1.676     albertel 6075: 		if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430     www      6076: 		    return 1;
                   6077: 		} else {
                   6078: 		    return 0;
                   6079: 		}
                   6080: 	    } else {
1.620     albertel 6081: 		return $env{'browser.'.$qualifier};
1.430     www      6082: 	    }
1.57      www      6083: # ------------------------------------------------------------ request.filename
                   6084:         } else {
1.620     albertel 6085:             return $env{'request.'.$spacequalifierrest};
1.29      www      6086:         }
1.28      www      6087:     } elsif ($realm eq 'course') {
1.48      www      6088: # ---------------------------------------------------------- course.description
1.620     albertel 6089:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      6090:     } elsif ($realm eq 'resource') {
1.165     www      6091: 
1.620     albertel 6092: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 6093: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   6094: 	}
1.693     albertel 6095: 
                   6096: 	if ($space eq 'title') {
                   6097: 	    if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   6098: 	    return &gettitle($symbparm);
                   6099: 	}
                   6100: 	
                   6101: 	if ($space eq 'map') {
                   6102: 	    my ($map) = &decode_symb($symbparm);
                   6103: 	    return &symbread($map);
                   6104: 	}
                   6105: 
                   6106: 	my ($section, $group, @groups);
1.593     albertel 6107: 	my ($courselevelm,$courselevel);
1.539     albertel 6108: 	if ($symbparm && defined($courseid) && 
1.620     albertel 6109: 	    $courseid eq $env{'request.course.id'}) {
1.165     www      6110: 
1.218     albertel 6111: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      6112: 
1.60      www      6113: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 6114: 	    my $symbp=$symbparm;
1.735     albertel 6115: 	    my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 6116: 
                   6117: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
                   6118: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
                   6119: 
1.620     albertel 6120: 	    if (($env{'user.name'} eq $uname) &&
                   6121: 		($env{'user.domain'} eq $udom)) {
                   6122: 		$section=$env{'request.course.sec'};
1.733     raeburn  6123:                 @groups = split(/:/,$env{'request.course.groups'});  
                   6124:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 6125: 	    } else {
1.539     albertel 6126: 		if (! defined($usection)) {
1.551     albertel 6127: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 6128: 		} else {
                   6129: 		    $section = $usection;
                   6130: 		}
1.733     raeburn  6131:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 6132: 	    }
                   6133: 
                   6134: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   6135: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
                   6136: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   6137: 
1.593     albertel 6138: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 6139: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.593     albertel 6140: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      6141: 
1.60      www      6142: # ----------------------------------------------------------- first, check user
1.624     albertel 6143: 
                   6144: 	    my $userreply=&resdata($uname,$udom,'user',
                   6145: 				       ($courselevelr,$courselevelm,
                   6146: 					$courselevel));
                   6147: 	    if (defined($userreply)) { return $userreply; }
1.95      www      6148: 
1.594     albertel 6149: # ------------------------------------------------ second, check some of course
1.684     raeburn  6150:             my $coursereply;
1.691     raeburn  6151:             if (@groups > 0) {
                   6152:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
                   6153:                                        $mapparm,$spacequalifierrest);
1.684     raeburn  6154:                 if (defined($coursereply)) { return $coursereply; }
                   6155:             }
1.96      www      6156: 
1.684     raeburn  6157: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.624     albertel 6158: 				     $env{'course.'.$courseid.'.domain'},
                   6159: 				     'course',
                   6160: 				     ($seclevelr,$seclevelm,$seclevel,
                   6161: 				      $courselevelr));
1.287     albertel 6162: 	    if (defined($coursereply)) { return $coursereply; }
1.200     www      6163: 
1.60      www      6164: # ------------------------------------------------------ third, check map parms
1.218     albertel 6165: 	    my %parmhash=();
                   6166: 	    my $thisparm='';
                   6167: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 6168: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 6169: 		    &GDBM_READER(),0640)) {
1.218     albertel 6170: 		$thisparm=$parmhash{$symbparm};
                   6171: 		untie(%parmhash);
                   6172: 	    }
                   6173: 	    if ($thisparm) { return $thisparm; }
                   6174: 	}
1.594     albertel 6175: # ------------------------------------------ fourth, look in resource metadata
1.71      www      6176: 
1.218     albertel 6177: 	$spacequalifierrest=~s/\./\_/;
1.282     albertel 6178: 	my $filename;
                   6179: 	if (!$symbparm) { $symbparm=&symbread(); }
                   6180: 	if ($symbparm) {
1.409     www      6181: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 6182: 	} else {
1.620     albertel 6183: 	    $filename=$env{'request.filename'};
1.282     albertel 6184: 	}
                   6185: 	my $metadata=&metadata($filename,$spacequalifierrest);
1.288     albertel 6186: 	if (defined($metadata)) { return $metadata; }
1.282     albertel 6187: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.288     albertel 6188: 	if (defined($metadata)) { return $metadata; }
1.142     www      6189: 
1.594     albertel 6190: # ---------------------------------------------- fourth, look in rest pf course
1.593     albertel 6191: 	if ($symbparm && defined($courseid) && 
1.620     albertel 6192: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 6193: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   6194: 				     $env{'course.'.$courseid.'.domain'},
                   6195: 				     'course',
                   6196: 				     ($courselevelm,$courselevel));
1.593     albertel 6197: 	    if (defined($coursereply)) { return $coursereply; }
                   6198: 	}
1.145     www      6199: # ------------------------------------------------------------------ Cascade up
1.218     albertel 6200: 	unless ($space eq '0') {
1.336     albertel 6201: 	    my @parts=split(/_/,$space);
                   6202: 	    my $id=pop(@parts);
                   6203: 	    my $part=join('_',@parts);
                   6204: 	    if ($part eq '') { $part='0'; }
                   6205: 	    my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 6206: 				 $symbparm,$udom,$uname,$section,1);
1.337     albertel 6207: 	    if (defined($partgeneral)) { return $partgeneral; }
1.218     albertel 6208: 	}
1.395     albertel 6209: 	if ($recurse) { return undef; }
                   6210: 	my $pack_def=&packages_tab_default($filename,$varname);
                   6211: 	if (defined($pack_def)) { return $pack_def; }
1.71      www      6212: 
1.48      www      6213: # ---------------------------------------------------- Any other user namespace
                   6214:     } elsif ($realm eq 'environment') {
                   6215: # ----------------------------------------------------------------- environment
1.620     albertel 6216: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   6217: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 6218: 	} else {
1.770     albertel 6219: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   6220: 		return '';
                   6221: 	    }
1.219     albertel 6222: 	    my %returnhash=&userenvironment($udom,$uname,
                   6223: 					    $spacequalifierrest);
                   6224: 	    return $returnhash{$spacequalifierrest};
                   6225: 	}
1.28      www      6226:     } elsif ($realm eq 'system') {
1.48      www      6227: # ----------------------------------------------------------------- system.time
                   6228: 	if ($space eq 'time') {
                   6229: 	    return time;
                   6230:         }
1.696     albertel 6231:     } elsif ($realm eq 'server') {
                   6232: # ----------------------------------------------------------------- system.time
                   6233: 	if ($space eq 'name') {
                   6234: 	    return $ENV{'SERVER_NAME'};
                   6235:         }
1.28      www      6236:     }
1.48      www      6237:     return '';
1.61      www      6238: }
                   6239: 
1.691     raeburn  6240: sub check_group_parms {
                   6241:     my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
                   6242:     my @groupitems = ();
                   6243:     my $resultitem;
                   6244:     my @levels = ($symbparm,$mapparm,$what);
                   6245:     foreach my $group (@{$groups}) {
                   6246:         foreach my $level (@levels) {
                   6247:              my $item = $courseid.'.['.$group.'].'.$level;
                   6248:              push(@groupitems,$item);
                   6249:         }
                   6250:     }
                   6251:     my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   6252:                             $env{'course.'.$courseid.'.domain'},
                   6253:                                      'course',@groupitems);
                   6254:     return $coursereply;
                   6255: }
                   6256: 
                   6257: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  6258:     my ($courseid,@groups) = @_;
                   6259:     @groups = sort(@groups);
1.691     raeburn  6260:     return @groups;
                   6261: }
                   6262: 
1.395     albertel 6263: sub packages_tab_default {
                   6264:     my ($uri,$varname)=@_;
                   6265:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 6266: 
                   6267:     my (@extension,@specifics,$do_default);
                   6268:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395     albertel 6269: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 6270: 	if ($pack_type eq 'default') {
                   6271: 	    $do_default=1;
                   6272: 	} elsif ($pack_type eq 'extension') {
                   6273: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.848     albertel 6274: 	} elsif ($pack_part eq $part) {
                   6275: 	    # only look at packages defaults for packages that this id is
1.738     albertel 6276: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   6277: 	}
                   6278:     }
                   6279:     # first look for a package that matches the requested part id
                   6280:     foreach my $package (@specifics) {
                   6281: 	my (undef,$pack_type,$pack_part)=@{$package};
                   6282: 	next if ($pack_part ne $part);
                   6283: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6284: 	    return $packagetab{"$pack_type&$name&default"};
                   6285: 	}
                   6286:     }
                   6287:     # look for any possible matching non extension_ package
                   6288:     foreach my $package (@specifics) {
                   6289: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 6290: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6291: 	    return $packagetab{"$pack_type&$name&default"};
                   6292: 	}
1.585     albertel 6293: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 6294: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   6295: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 6296: 	}
                   6297:     }
1.738     albertel 6298:     # look for any posible extension_ match
                   6299:     foreach my $package (@extension) {
                   6300: 	my ($package,$pack_type)=@{$package};
                   6301: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6302: 	    return $packagetab{"$pack_type&$name&default"};
                   6303: 	}
                   6304: 	if (defined($packagetab{$package."&$name&default"})) {
                   6305: 	    return $packagetab{$package."&$name&default"};
                   6306: 	}
                   6307:     }
                   6308:     # look for a global default setting
                   6309:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   6310: 	return $packagetab{"default&$name&default"};
                   6311:     }
1.395     albertel 6312:     return undef;
                   6313: }
                   6314: 
1.334     albertel 6315: sub add_prefix_and_part {
                   6316:     my ($prefix,$part)=@_;
                   6317:     my $keyroot;
                   6318:     if (defined($prefix) && $prefix !~ /^__/) {
                   6319: 	# prefix that has a part already
                   6320: 	$keyroot=$prefix;
                   6321:     } elsif (defined($prefix)) {
                   6322: 	# prefix that is missing a part
                   6323: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   6324:     } else {
                   6325: 	# no prefix at all
                   6326: 	if (defined($part)) { $keyroot='_'.$part; }
                   6327:     }
                   6328:     return $keyroot;
                   6329: }
                   6330: 
1.71      www      6331: # ---------------------------------------------------------------- Get metadata
                   6332: 
1.599     albertel 6333: my %metaentry;
1.71      www      6334: sub metadata {
1.176     www      6335:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71      www      6336:     $uri=&declutter($uri);
1.288     albertel 6337:     # if it is a non metadata possible uri return quickly
1.529     albertel 6338:     if (($uri eq '') || 
                   6339: 	(($uri =~ m|^/*adm/|) && 
1.698     albertel 6340: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.423     albertel 6341:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ /^~/) ||
1.807     albertel 6342: 	($uri =~ m|home/$match_username/public_html/|)) {
1.468     albertel 6343: 	return undef;
1.288     albertel 6344:     }
1.73      www      6345:     my $filename=$uri;
                   6346:     $uri=~s/\.meta$//;
1.172     www      6347: #
                   6348: # Is the metadata already cached?
1.177     www      6349: # Look at timestamp of caching
1.172     www      6350: # Everything is cached by the main uri, libraries are never directly cached
                   6351: #
1.428     albertel 6352:     if (!defined($liburi)) {
1.599     albertel 6353: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 6354: 	if (defined($cached)) { return $result->{':'.$what}; }
                   6355:     }
                   6356:     {
1.172     www      6357: #
                   6358: # Is this a recursive call for a library?
                   6359: #
1.599     albertel 6360: #	if (! exists($metacache{$uri})) {
                   6361: #	    $metacache{$uri}={};
                   6362: #	}
1.171     www      6363:         if ($liburi) {
                   6364: 	    $liburi=&declutter($liburi);
                   6365:             $filename=$liburi;
1.401     bowersj2 6366:         } else {
1.599     albertel 6367: 	    &devalidate_cache_new('meta',$uri);
                   6368: 	    undef(%metaentry);
1.401     bowersj2 6369: 	}
1.140     www      6370:         my %metathesekeys=();
1.73      www      6371:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 6372: 	my $metastring;
1.768     albertel 6373: 	if ($uri !~ m -^(editupload)/-) {
1.543     albertel 6374: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 6375: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 6376: 	    $metastring=&getfile($file);
1.489     albertel 6377: 	}
1.208     albertel 6378:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      6379:         my $token;
1.140     www      6380:         undef %metathesekeys;
1.71      www      6381:         while ($token=$parser->get_token) {
1.339     albertel 6382: 	    if ($token->[0] eq 'S') {
                   6383: 		if (defined($token->[2]->{'package'})) {
1.172     www      6384: #
                   6385: # This is a package - get package info
                   6386: #
1.339     albertel 6387: 		    my $package=$token->[2]->{'package'};
                   6388: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   6389: 		    if (defined($token->[2]->{'id'})) { 
                   6390: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   6391: 		    }
1.599     albertel 6392: 		    if ($metaentry{':packages'}) {
                   6393: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 6394: 		    } else {
1.599     albertel 6395: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 6396: 		    }
1.736     albertel 6397: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 6398: 			my $part=$keyroot;
                   6399: 			$part=~s/^\_//;
1.736     albertel 6400: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   6401: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   6402: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 6403: 			    # ignore package.tab specified default values
                   6404:                             # here &package_tab_default() will fetch those
                   6405: 			    if ($subp eq 'default') { next; }
1.736     albertel 6406: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 6407: 			    my $unikey;
                   6408: 			    if ($pack =~ /_0$/) {
                   6409: 				$unikey='parameter_0_'.$name;
                   6410: 				$part=0;
                   6411: 			    } else {
                   6412: 				$unikey='parameter'.$keyroot.'_'.$name;
                   6413: 			    }
1.339     albertel 6414: 			    if ($subp eq 'display') {
                   6415: 				$value.=' [Part: '.$part.']';
                   6416: 			    }
1.599     albertel 6417: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 6418: 			    $metathesekeys{$unikey}=1;
1.599     albertel 6419: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   6420: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 6421: 			    }
1.599     albertel 6422: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   6423: 				$metaentry{':'.$unikey}=
                   6424: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 6425: 			    }
1.339     albertel 6426: 			}
                   6427: 		    }
                   6428: 		} else {
1.172     www      6429: #
                   6430: # This is not a package - some other kind of start tag
1.339     albertel 6431: #
                   6432: 		    my $entry=$token->[1];
                   6433: 		    my $unikey;
                   6434: 		    if ($entry eq 'import') {
                   6435: 			$unikey='';
                   6436: 		    } else {
                   6437: 			$unikey=$entry;
                   6438: 		    }
                   6439: 		    $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   6440: 
                   6441: 		    if (defined($token->[2]->{'id'})) { 
                   6442: 			$unikey.='_'.$token->[2]->{'id'}; 
                   6443: 		    }
1.175     www      6444: 
1.339     albertel 6445: 		    if ($entry eq 'import') {
1.175     www      6446: #
                   6447: # Importing a library here
1.339     albertel 6448: #
                   6449: 			if ($depthcount<20) {
                   6450: 			    my $location=$parser->get_text('/import');
                   6451: 			    my $dir=$filename;
                   6452: 			    $dir=~s|[^/]*$||;
                   6453: 			    $location=&filelocation($dir,$location);
1.736     albertel 6454: 			    my $metadata = 
                   6455: 				&metadata($uri,'keys', $location,$unikey,
                   6456: 					  $depthcount+1);
                   6457: 			    foreach my $meta (split(',',$metadata)) {
                   6458: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   6459: 				$metathesekeys{$meta}=1;
1.339     albertel 6460: 			    }
                   6461: 			}
                   6462: 		    } else { 
                   6463: 			
                   6464: 			if (defined($token->[2]->{'name'})) { 
                   6465: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   6466: 			}
                   6467: 			$metathesekeys{$unikey}=1;
1.736     albertel 6468: 			foreach my $param (@{$token->[3]}) {
                   6469: 			    $metaentry{':'.$unikey.'.'.$param} =
                   6470: 				$token->[2]->{$param};
1.339     albertel 6471: 			}
                   6472: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 6473: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 6474: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   6475: 		 # only ws inside the tag, and not in default, so use default
                   6476: 		 # as value
1.599     albertel 6477: 			    $metaentry{':'.$unikey}=$default;
1.339     albertel 6478: 			} else {
1.321     albertel 6479: 		  # either something interesting inside the tag or default
                   6480:                   # uninteresting
1.599     albertel 6481: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 6482: 			}
1.172     www      6483: # end of not-a-package not-a-library import
1.339     albertel 6484: 		    }
1.172     www      6485: # end of not-a-package start tag
1.339     albertel 6486: 		}
1.172     www      6487: # the next is the end of "start tag"
1.339     albertel 6488: 	    }
                   6489: 	}
1.483     albertel 6490: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.737     albertel 6491: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 6492: 	    #no specific packages #how's our extension
                   6493: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 6494: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 6495: 					 \%metathesekeys);
                   6496: 	}
1.599     albertel 6497: 	if (!exists($metaentry{':packages'})) {
1.737     albertel 6498: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 6499: 		#no specific packages well let's get default then
                   6500: 		if ($key!~/^default&/) { next; }
1.488     albertel 6501: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 6502: 					     \%metathesekeys);
                   6503: 	    }
                   6504: 	}
1.338     www      6505: # are there custom rights to evaluate
1.599     albertel 6506: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 6507: 
1.338     www      6508:     #
                   6509:     # Importing a rights file here
1.339     albertel 6510:     #
                   6511: 	    unless ($depthcount) {
1.599     albertel 6512: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 6513: 		my $dir=$filename;
                   6514: 		$dir=~s|[^/]*$||;
                   6515: 		$location=&filelocation($dir,$location);
1.736     albertel 6516: 		my $rights_metadata =
                   6517: 		    &metadata($uri,'keys',$location,'_rights',
                   6518: 			      $depthcount+1);
                   6519: 		foreach my $rights (split(',',$rights_metadata)) {
                   6520: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   6521: 		    $metathesekeys{$rights}=1;
1.339     albertel 6522: 		}
                   6523: 	    }
                   6524: 	}
1.737     albertel 6525: 	# uniqifiy package listing
                   6526: 	my %seen;
                   6527: 	my @uniq_packages =
                   6528: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   6529: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   6530: 
                   6531: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 6532: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
                   6533: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.699     albertel 6534: 	&do_cache_new('meta',$uri,\%metaentry,60*60);
1.177     www      6535: # this is the end of "was not already recently cached
1.71      www      6536:     }
1.599     albertel 6537:     return $metaentry{':'.$what};
1.261     albertel 6538: }
                   6539: 
1.488     albertel 6540: sub metadata_create_package_def {
1.483     albertel 6541:     my ($uri,$key,$package,$metathesekeys)=@_;
                   6542:     my ($pack,$name,$subp)=split(/\&/,$key);
                   6543:     if ($subp eq 'default') { next; }
                   6544:     
1.599     albertel 6545:     if (defined($metaentry{':packages'})) {
                   6546: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 6547:     } else {
1.599     albertel 6548: 	$metaentry{':packages'}=$package;
1.483     albertel 6549:     }
                   6550:     my $value=$packagetab{$key};
                   6551:     my $unikey;
                   6552:     $unikey='parameter_0_'.$name;
1.599     albertel 6553:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 6554:     $$metathesekeys{$unikey}=1;
1.599     albertel 6555:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   6556: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 6557:     }
1.599     albertel 6558:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   6559: 	$metaentry{':'.$unikey}=
                   6560: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 6561:     }
                   6562: }
                   6563: 
1.261     albertel 6564: sub metadata_generate_part0 {
                   6565:     my ($metadata,$metacache,$uri) = @_;
                   6566:     my %allnames;
1.737     albertel 6567:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 6568: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 6569: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   6570: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 6571: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 6572: 	    $allnames{$name}=$part;
                   6573: 	  }
                   6574: 	}
                   6575:     }
                   6576:     foreach my $name (keys(%allnames)) {
                   6577:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 6578:       my $key=":parameter_0_$name";
1.261     albertel 6579:       $$metacache{"$key.part"}='0';
                   6580:       $$metacache{"$key.name"}=$name;
1.428     albertel 6581:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 6582: 					   $allnames{$name}.'_'.$name.
                   6583: 					   '.type'};
1.428     albertel 6584:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 6585: 			     '.display'};
1.644     www      6586:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 6587:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 6588:       $$metacache{"$key.display"}=$olddis;
                   6589:     }
1.71      www      6590: }
                   6591: 
1.764     albertel 6592: # ------------------------------------------------------ Devalidate title cache
                   6593: 
                   6594: sub devalidate_title_cache {
                   6595:     my ($url)=@_;
                   6596:     if (!$env{'request.course.id'}) { return; }
                   6597:     my $symb=&symbread($url);
                   6598:     if (!$symb) { return; }
                   6599:     my $key=$env{'request.course.id'}."\0".$symb;
                   6600:     &devalidate_cache_new('title',$key);
                   6601: }
                   6602: 
1.301     www      6603: # ------------------------------------------------- Get the title of a resource
                   6604: 
                   6605: sub gettitle {
                   6606:     my $urlsymb=shift;
                   6607:     my $symb=&symbread($urlsymb);
1.534     albertel 6608:     if ($symb) {
1.620     albertel 6609: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 6610: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 6611: 	if (defined($cached)) { 
                   6612: 	    return $result;
                   6613: 	}
1.534     albertel 6614: 	my ($map,$resid,$url)=&decode_symb($symb);
                   6615: 	my $title='';
                   6616: 	my %bighash;
1.620     albertel 6617: 	if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.534     albertel 6618: 		&GDBM_READER(),0640)) {
                   6619: 	    my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   6620: 	    $title=$bighash{'title_'.$mapid.'.'.$resid};
                   6621: 	    untie %bighash;
                   6622: 	}
                   6623: 	$title=~s/\&colon\;/\:/gs;
                   6624: 	if ($title) {
1.599     albertel 6625: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 6626: 	}
                   6627: 	$urlsymb=$url;
                   6628:     }
                   6629:     my $title=&metadata($urlsymb,'title');
                   6630:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   6631:     return $title;
1.301     www      6632: }
1.613     albertel 6633: 
1.614     albertel 6634: sub get_slot {
                   6635:     my ($which,$cnum,$cdom)=@_;
                   6636:     if (!$cnum || !$cdom) {
1.790     albertel 6637: 	(undef,my $courseid)=&whichuser();
1.620     albertel 6638: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   6639: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 6640:     }
1.703     albertel 6641:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   6642:     my %slotinfo;
                   6643:     if (exists($remembered{$key})) {
                   6644: 	$slotinfo{$which} = $remembered{$key};
                   6645:     } else {
                   6646: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   6647: 	&Apache::lonhomework::showhash(%slotinfo);
                   6648: 	my ($tmp)=keys(%slotinfo);
                   6649: 	if ($tmp=~/^error:/) { return (); }
                   6650: 	$remembered{$key} = $slotinfo{$which};
                   6651:     }
1.616     albertel 6652:     if (ref($slotinfo{$which}) eq 'HASH') {
                   6653: 	return %{$slotinfo{$which}};
                   6654:     }
                   6655:     return $slotinfo{$which};
1.614     albertel 6656: }
1.31      www      6657: # ------------------------------------------------- Update symbolic store links
                   6658: 
                   6659: sub symblist {
                   6660:     my ($mapname,%newhash)=@_;
1.438     www      6661:     $mapname=&deversion(&declutter($mapname));
1.31      www      6662:     my %hash;
1.620     albertel 6663:     if (($env{'request.course.fn'}) && (%newhash)) {
                   6664:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 6665:                       &GDBM_WRCREAT(),0640)) {
1.711     albertel 6666: 	    foreach my $url (keys %newhash) {
                   6667: 		next if ($url eq 'last_known'
                   6668: 			 && $env{'form.no_update_last_known'});
                   6669: 		$hash{declutter($url)}=&encode_symb($mapname,
                   6670: 						    $newhash{$url}->[1],
                   6671: 						    $newhash{$url}->[0]);
1.191     harris41 6672:             }
1.31      www      6673:             if (untie(%hash)) {
                   6674: 		return 'ok';
                   6675:             }
                   6676:         }
                   6677:     }
                   6678:     return 'error';
1.212     www      6679: }
                   6680: 
                   6681: # --------------------------------------------------------------- Verify a symb
                   6682: 
                   6683: sub symbverify {
1.510     www      6684:     my ($symb,$thisurl)=@_;
                   6685:     my $thisfn=$thisurl;
1.439     www      6686:     $thisfn=&declutter($thisfn);
1.215     www      6687: # direct jump to resource in page or to a sequence - will construct own symbs
                   6688:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   6689: # check URL part
1.409     www      6690:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      6691: 
1.431     www      6692:     unless ($url eq $thisfn) { return 0; }
1.213     www      6693: 
1.216     www      6694:     $symb=&symbclean($symb);
1.510     www      6695:     $thisurl=&deversion($thisurl);
1.439     www      6696:     $thisfn=&deversion($thisfn);
1.213     www      6697: 
                   6698:     my %bighash;
                   6699:     my $okay=0;
1.431     www      6700: 
1.620     albertel 6701:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 6702:                             &GDBM_READER(),0640)) {
1.510     www      6703:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216     www      6704:         unless ($ids) { 
1.510     www      6705:            $ids=$bighash{'ids_/'.$thisurl};
1.216     www      6706:         }
                   6707:         if ($ids) {
                   6708: # ------------------------------------------------------------------- Has ID(s)
1.800     albertel 6709: 	    foreach my $id (split(/\,/,$ids)) {
                   6710: 	       my ($mapid,$resid)=split(/\./,$id);
1.216     www      6711:                if (
                   6712:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
                   6713:    eq $symb) { 
1.620     albertel 6714: 		   if (($env{'request.role.adv'}) ||
1.800     albertel 6715: 		       $bighash{'encrypted_'.$id} eq $env{'request.enc'}) {
1.582     albertel 6716: 		       $okay=1; 
                   6717: 		   }
                   6718: 	       }
1.216     www      6719: 	   }
                   6720:         }
1.213     www      6721: 	untie(%bighash);
                   6722:     }
                   6723:     return $okay;
1.31      www      6724: }
                   6725: 
1.210     www      6726: # --------------------------------------------------------------- Clean-up symb
                   6727: 
                   6728: sub symbclean {
                   6729:     my $symb=shift;
1.568     albertel 6730:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      6731: # remove version from map
                   6732:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      6733: 
1.210     www      6734: # remove version from URL
                   6735:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      6736: 
1.507     www      6737: # remove wrapper
                   6738: 
1.510     www      6739:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 6740:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      6741:     return $symb;
1.409     www      6742: }
                   6743: 
                   6744: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 6745: 
                   6746: sub encode_symb {
                   6747:     my ($map,$resid,$url)=@_;
                   6748:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   6749: }
1.409     www      6750: 
                   6751: sub decode_symb {
1.568     albertel 6752:     my $symb=shift;
                   6753:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   6754:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      6755:     return (&fixversion($map),$resid,&fixversion($url));
                   6756: }
                   6757: 
                   6758: sub fixversion {
                   6759:     my $fn=shift;
1.609     banghart 6760:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      6761:     my %bighash;
                   6762:     my $uri=&clutter($fn);
1.620     albertel 6763:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      6764: # is this cached?
1.599     albertel 6765:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      6766:     if (defined($cached)) { return $result; }
                   6767: # unfortunately not cached, or expired
1.620     albertel 6768:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      6769: 	    &GDBM_READER(),0640)) {
                   6770:  	if ($bighash{'version_'.$uri}) {
                   6771:  	    my $version=$bighash{'version_'.$uri};
1.444     www      6772:  	    unless (($version eq 'mostrecent') || 
                   6773: 		    ($version==&getversion($uri))) {
1.440     www      6774:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   6775:  	    }
                   6776:  	}
                   6777:  	untie %bighash;
1.413     www      6778:     }
1.599     albertel 6779:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      6780: }
                   6781: 
                   6782: sub deversion {
                   6783:     my $url=shift;
                   6784:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   6785:     return $url;
1.210     www      6786: }
                   6787: 
1.31      www      6788: # ------------------------------------------------------ Return symb list entry
                   6789: 
                   6790: sub symbread {
1.249     www      6791:     my ($thisfn,$donotrecurse)=@_;
1.542     albertel 6792:     my $cache_str='request.symbread.cached.'.$thisfn;
1.620     albertel 6793:     if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242     www      6794: # no filename provided? try from environment
1.44      www      6795:     unless ($thisfn) {
1.620     albertel 6796:         if ($env{'request.symb'}) {
                   6797: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 6798: 	}
1.620     albertel 6799: 	$thisfn=$env{'request.filename'};
1.44      www      6800:     }
1.569     albertel 6801:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      6802: # is that filename actually a symb? Verify, clean, and return
                   6803:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 6804: 	if (&symbverify($thisfn,$1)) {
1.620     albertel 6805: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 6806: 	}
1.242     www      6807:     }
1.44      www      6808:     $thisfn=declutter($thisfn);
1.31      www      6809:     my %hash;
1.37      www      6810:     my %bighash;
                   6811:     my $syval='';
1.620     albertel 6812:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  6813:         my $targetfn = $thisfn;
1.609     banghart 6814:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  6815:             $targetfn = 'adm/wrapper/'.$thisfn;
                   6816:         }
1.687     albertel 6817: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   6818: 	    $targetfn=$1;
                   6819: 	}
1.620     albertel 6820:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 6821:                       &GDBM_READER(),0640)) {
1.481     raeburn  6822: 	    $syval=$hash{$targetfn};
1.37      www      6823:             untie(%hash);
                   6824:         }
                   6825: # ---------------------------------------------------------- There was an entry
                   6826:         if ($syval) {
1.601     albertel 6827: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 6828: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.601     albertel 6829: 		    #&appenv('request.ambiguous' => $thisfn);
1.620     albertel 6830: 		    #return $env{$cache_str}='';
1.601     albertel 6831: 		#}    
                   6832: 		#$syval.=$1;
                   6833: 	    #}
1.37      www      6834:         } else {
                   6835: # ------------------------------------------------------- Was not in symb table
1.620     albertel 6836:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 6837:                             &GDBM_READER(),0640)) {
1.37      www      6838: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      6839:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      6840:               unless ($ids) { 
                   6841:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      6842:               }
                   6843:               unless ($ids) {
                   6844: # alias?
                   6845: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      6846:               }
1.37      www      6847:               if ($ids) {
                   6848: # ------------------------------------------------------------------- Has ID(s)
                   6849:                  my @possibilities=split(/\,/,$ids);
1.39      www      6850:                  if ($#possibilities==0) {
                   6851: # ----------------------------------------------- There is only one possibility
1.37      www      6852: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 6853: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   6854: 						    $resid,$thisfn);
1.249     www      6855:                  } elsif (!$donotrecurse) {
1.39      www      6856: # ------------------------------------------ There is more than one possibility
                   6857:                      my $realpossible=0;
1.800     albertel 6858:                      foreach my $id (@possibilities) {
                   6859: 			 my $file=$bighash{'src_'.$id};
1.39      www      6860:                          if (&allowed('bre',$file)) {
1.800     albertel 6861:          		    my ($mapid,$resid)=split(/\./,$id);
1.39      www      6862:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
                   6863: 				$realpossible++;
1.626     albertel 6864:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   6865: 						    $resid,$thisfn);
1.39      www      6866:                             }
                   6867: 			 }
1.191     harris41 6868:                      }
1.39      www      6869: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      6870:                  } else {
                   6871:                      $syval='';
1.37      www      6872:                  }
                   6873: 	      }
                   6874:               untie(%bighash)
1.481     raeburn  6875:            }
1.31      www      6876:         }
1.62      www      6877:         if ($syval) {
1.620     albertel 6878: 	    return $env{$cache_str}=$syval;
1.62      www      6879:         }
1.31      www      6880:     }
1.44      www      6881:     &appenv('request.ambiguous' => $thisfn);
1.620     albertel 6882:     return $env{$cache_str}='';
1.31      www      6883: }
                   6884: 
                   6885: # ---------------------------------------------------------- Return random seed
                   6886: 
1.32      www      6887: sub numval {
                   6888:     my $txt=shift;
                   6889:     $txt=~tr/A-J/0-9/;
                   6890:     $txt=~tr/a-j/0-9/;
                   6891:     $txt=~tr/K-T/0-9/;
                   6892:     $txt=~tr/k-t/0-9/;
                   6893:     $txt=~tr/U-Z/0-5/;
                   6894:     $txt=~tr/u-z/0-5/;
                   6895:     $txt=~s/\D//g;
1.564     albertel 6896:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      6897:     return int($txt);
1.368     albertel 6898: }
                   6899: 
1.484     albertel 6900: sub numval2 {
                   6901:     my $txt=shift;
                   6902:     $txt=~tr/A-J/0-9/;
                   6903:     $txt=~tr/a-j/0-9/;
                   6904:     $txt=~tr/K-T/0-9/;
                   6905:     $txt=~tr/k-t/0-9/;
                   6906:     $txt=~tr/U-Z/0-5/;
                   6907:     $txt=~tr/u-z/0-5/;
                   6908:     $txt=~s/\D//g;
                   6909:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   6910:     my $total;
                   6911:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 6912:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 6913:     return int($total);
                   6914: }
                   6915: 
1.575     albertel 6916: sub numval3 {
                   6917:     use integer;
                   6918:     my $txt=shift;
                   6919:     $txt=~tr/A-J/0-9/;
                   6920:     $txt=~tr/a-j/0-9/;
                   6921:     $txt=~tr/K-T/0-9/;
                   6922:     $txt=~tr/k-t/0-9/;
                   6923:     $txt=~tr/U-Z/0-5/;
                   6924:     $txt=~tr/u-z/0-5/;
                   6925:     $txt=~s/\D//g;
                   6926:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   6927:     my $total;
                   6928:     foreach my $val (@txts) { $total+=$val; }
                   6929:     if ($_64bit) { $total=(($total<<32)>>32); }
                   6930:     return $total;
                   6931: }
                   6932: 
1.675     albertel 6933: sub digest {
                   6934:     my ($data)=@_;
                   6935:     my $digest=&Digest::MD5::md5($data);
                   6936:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   6937:     my ($e,$f);
                   6938:     {
                   6939:         use integer;
                   6940:         $e=($a+$b);
                   6941:         $f=($c+$d);
                   6942:         if ($_64bit) {
                   6943:             $e=(($e<<32)>>32);
                   6944:             $f=(($f<<32)>>32);
                   6945:         }
                   6946:     }
                   6947:     if (wantarray) {
                   6948: 	return ($e,$f);
                   6949:     } else {
                   6950: 	my $g;
                   6951: 	{
                   6952: 	    use integer;
                   6953: 	    $g=($e+$f);
                   6954: 	    if ($_64bit) {
                   6955: 		$g=(($g<<32)>>32);
                   6956: 	    }
                   6957: 	}
                   6958: 	return $g;
                   6959:     }
                   6960: }
                   6961: 
1.368     albertel 6962: sub latest_rnd_algorithm_id {
1.675     albertel 6963:     return '64bit5';
1.366     albertel 6964: }
1.32      www      6965: 
1.503     albertel 6966: sub get_rand_alg {
                   6967:     my ($courseid)=@_;
1.790     albertel 6968:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 6969:     if ($courseid) {
1.620     albertel 6970: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 6971:     }
                   6972:     return &latest_rnd_algorithm_id();
                   6973: }
                   6974: 
1.562     albertel 6975: sub validCODE {
                   6976:     my ($CODE)=@_;
                   6977:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   6978:     return 0;
                   6979: }
                   6980: 
1.491     albertel 6981: sub getCODE {
1.620     albertel 6982:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 6983:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   6984: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   6985: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 6986: 	return $Apache::lonhomework::history{'resource.CODE'};
                   6987:     }
                   6988:     return undef;
                   6989: }
                   6990: 
1.31      www      6991: sub rndseed {
1.155     albertel 6992:     my ($symb,$courseid,$domain,$username)=@_;
1.790     albertel 6993:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.155     albertel 6994:     if (!$symb) {
1.366     albertel 6995: 	unless ($symb=$wsymb) { return time; }
                   6996:     }
                   6997:     if (!$courseid) { $courseid=$wcourseid; }
                   6998:     if (!$domain) { $domain=$wdomain; }
                   6999:     if (!$username) { $username=$wusername }
1.503     albertel 7000:     my $which=&get_rand_alg();
1.803     albertel 7001: 
1.491     albertel 7002:     if (defined(&getCODE())) {
1.675     albertel 7003: 	if ($which eq '64bit5') {
                   7004: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   7005: 	} elsif ($which eq '64bit4') {
1.575     albertel 7006: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   7007: 	} else {
                   7008: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   7009: 	}
1.675     albertel 7010:     } elsif ($which eq '64bit5') {
                   7011: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 7012:     } elsif ($which eq '64bit4') {
                   7013: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 7014:     } elsif ($which eq '64bit3') {
                   7015: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 7016:     } elsif ($which eq '64bit2') {
                   7017: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 7018:     } elsif ($which eq '64bit') {
                   7019: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   7020:     }
                   7021:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   7022: }
                   7023: 
                   7024: sub rndseed_32bit {
                   7025:     my ($symb,$courseid,$domain,$username)=@_;
                   7026:     {
                   7027: 	use integer;
                   7028: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   7029: 	my $symbseed=numval($symb) << 22;
                   7030: 	my $namechck=unpack("%32C*",$username) << 17;
                   7031: 	my $nameseed=numval($username) << 12;
                   7032: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   7033: 	my $courseseed=unpack("%32C*",$courseid);
                   7034: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 7035: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7036: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 7037: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 7038: 	return $num;
                   7039:     }
                   7040: }
                   7041: 
                   7042: sub rndseed_64bit {
                   7043:     my ($symb,$courseid,$domain,$username)=@_;
                   7044:     {
                   7045: 	use integer;
                   7046: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   7047: 	my $symbseed=numval($symb) << 10;
                   7048: 	my $namechck=unpack("%32S*",$username);
                   7049: 	
                   7050: 	my $nameseed=numval($username) << 21;
                   7051: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   7052: 	my $courseseed=unpack("%32S*",$courseid);
                   7053: 	
                   7054: 	my $num1=$symbchck+$symbseed+$namechck;
                   7055: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7056: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7057: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 7058: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 7059: 	return "$num1,$num2";
1.155     albertel 7060:     }
1.366     albertel 7061: }
                   7062: 
1.443     albertel 7063: sub rndseed_64bit2 {
                   7064:     my ($symb,$courseid,$domain,$username)=@_;
                   7065:     {
                   7066: 	use integer;
                   7067: 	# strings need to be an even # of cahracters long, it it is odd the
                   7068:         # last characters gets thrown away
                   7069: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7070: 	my $symbseed=numval($symb) << 10;
                   7071: 	my $namechck=unpack("%32S*",$username.' ');
                   7072: 	
                   7073: 	my $nameseed=numval($username) << 21;
1.501     albertel 7074: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7075: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7076: 	
                   7077: 	my $num1=$symbchck+$symbseed+$namechck;
                   7078: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7079: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7080: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 7081: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 7082: 	return "$num1,$num2";
                   7083:     }
                   7084: }
                   7085: 
                   7086: sub rndseed_64bit3 {
                   7087:     my ($symb,$courseid,$domain,$username)=@_;
                   7088:     {
                   7089: 	use integer;
                   7090: 	# strings need to be an even # of cahracters long, it it is odd the
                   7091:         # last characters gets thrown away
                   7092: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7093: 	my $symbseed=numval2($symb) << 10;
                   7094: 	my $namechck=unpack("%32S*",$username.' ');
                   7095: 	
                   7096: 	my $nameseed=numval2($username) << 21;
1.443     albertel 7097: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7098: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7099: 	
                   7100: 	my $num1=$symbchck+$symbseed+$namechck;
                   7101: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7102: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7103: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 7104: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
                   7105: 	
1.503     albertel 7106: 	return "$num1:$num2";
1.443     albertel 7107:     }
                   7108: }
                   7109: 
1.575     albertel 7110: sub rndseed_64bit4 {
                   7111:     my ($symb,$courseid,$domain,$username)=@_;
                   7112:     {
                   7113: 	use integer;
                   7114: 	# strings need to be an even # of cahracters long, it it is odd the
                   7115:         # last characters gets thrown away
                   7116: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7117: 	my $symbseed=numval3($symb) << 10;
                   7118: 	my $namechck=unpack("%32S*",$username.' ');
                   7119: 	
                   7120: 	my $nameseed=numval3($username) << 21;
                   7121: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7122: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7123: 	
                   7124: 	my $num1=$symbchck+$symbseed+$namechck;
                   7125: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7126: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7127: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 7128: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
                   7129: 	
                   7130: 	return "$num1:$num2";
                   7131:     }
                   7132: }
                   7133: 
1.675     albertel 7134: sub rndseed_64bit5 {
                   7135:     my ($symb,$courseid,$domain,$username)=@_;
                   7136:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   7137:     return "$num1:$num2";
                   7138: }
                   7139: 
1.366     albertel 7140: sub rndseed_CODE_64bit {
                   7141:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 7142:     {
1.366     albertel 7143: 	use integer;
1.443     albertel 7144: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 7145: 	my $symbseed=numval2($symb);
1.491     albertel 7146: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   7147: 	my $CODEseed=numval(&getCODE());
1.443     albertel 7148: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 7149: 	my $num1=$symbseed+$CODEchck;
                   7150: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 7151: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   7152: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 7153: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   7154: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 7155: 	return "$num1:$num2";
1.366     albertel 7156:     }
                   7157: }
                   7158: 
1.575     albertel 7159: sub rndseed_CODE_64bit4 {
                   7160:     my ($symb,$courseid,$domain,$username)=@_;
                   7161:     {
                   7162: 	use integer;
                   7163: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   7164: 	my $symbseed=numval3($symb);
                   7165: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   7166: 	my $CODEseed=numval3(&getCODE());
                   7167: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7168: 	my $num1=$symbseed+$CODEchck;
                   7169: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 7170: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   7171: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 7172: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   7173: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   7174: 	return "$num1:$num2";
                   7175:     }
                   7176: }
                   7177: 
1.675     albertel 7178: sub rndseed_CODE_64bit5 {
                   7179:     my ($symb,$courseid,$domain,$username)=@_;
                   7180:     my $code = &getCODE();
                   7181:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   7182:     return "$num1:$num2";
                   7183: }
                   7184: 
1.366     albertel 7185: sub setup_random_from_rndseed {
                   7186:     my ($rndseed)=@_;
1.503     albertel 7187:     if ($rndseed =~/([,:])/) {
                   7188: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366     albertel 7189: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
                   7190:     } else {
                   7191: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 7192:     }
1.36      albertel 7193: }
                   7194: 
1.474     albertel 7195: sub latest_receipt_algorithm_id {
1.835     albertel 7196:     return 'receipt3';
1.474     albertel 7197: }
                   7198: 
1.480     www      7199: sub recunique {
                   7200:     my $fucourseid=shift;
                   7201:     my $unique;
1.835     albertel 7202:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   7203: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 7204: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      7205:     } else {
                   7206: 	$unique=$perlvar{'lonReceipt'};
                   7207:     }
                   7208:     return unpack("%32C*",$unique);
                   7209: }
                   7210: 
                   7211: sub recprefix {
                   7212:     my $fucourseid=shift;
                   7213:     my $prefix;
1.835     albertel 7214:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   7215: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 7216: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      7217:     } else {
                   7218: 	$prefix=$perlvar{'lonHostID'};
                   7219:     }
                   7220:     return unpack("%32C*",$prefix);
                   7221: }
                   7222: 
1.76      www      7223: sub ireceipt {
1.474     albertel 7224:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 7225: 
                   7226:     my $return =&recprefix($fucourseid).'-';
                   7227: 
                   7228:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   7229: 	$env{'request.state'} eq 'construct') {
                   7230: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   7231: 	return $return;
                   7232:     }
                   7233: 
1.76      www      7234:     my $cuname=unpack("%32C*",$funame);
                   7235:     my $cudom=unpack("%32C*",$fudom);
                   7236:     my $cucourseid=unpack("%32C*",$fucourseid);
                   7237:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      7238:     my $cunique=&recunique($fucourseid);
1.474     albertel 7239:     my $cpart=unpack("%32S*",$part);
1.835     albertel 7240:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   7241: 
1.790     albertel 7242: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 7243: 			       
                   7244: 	$return.= ($cunique%$cuname+
                   7245: 		   $cunique%$cudom+
                   7246: 		   $cusymb%$cuname+
                   7247: 		   $cusymb%$cudom+
                   7248: 		   $cucourseid%$cuname+
                   7249: 		   $cucourseid%$cudom+
                   7250: 		   $cpart%$cuname+
                   7251: 		   $cpart%$cudom);
                   7252:     } else {
                   7253: 	$return.= ($cunique%$cuname+
                   7254: 		   $cunique%$cudom+
                   7255: 		   $cusymb%$cuname+
                   7256: 		   $cusymb%$cudom+
                   7257: 		   $cucourseid%$cuname+
                   7258: 		   $cucourseid%$cudom);
                   7259:     }
                   7260:     return $return;
1.76      www      7261: }
                   7262: 
                   7263: sub receipt {
1.474     albertel 7264:     my ($part)=@_;
1.790     albertel 7265:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 7266:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      7267: }
1.260     ng       7268: 
1.790     albertel 7269: sub whichuser {
                   7270:     my ($passedsymb)=@_;
                   7271:     my ($symb,$courseid,$domain,$name,$publicuser);
                   7272:     if (defined($env{'form.grade_symb'})) {
                   7273: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   7274: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   7275: 	if (!$allowed &&
                   7276: 	    exists($env{'request.course.sec'}) &&
                   7277: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   7278: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   7279: 			      '/'.$env{'request.course.sec'});
                   7280: 	}
                   7281: 	if ($allowed) {
                   7282: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   7283: 	    $courseid=$tmp_courseid;
                   7284: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   7285: 	    ($name)=&get_env_multiple('form.grade_username');
                   7286: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   7287: 	}
                   7288:     }
                   7289:     if (!$passedsymb) {
                   7290: 	$symb=&symbread();
                   7291:     } else {
                   7292: 	$symb=$passedsymb;
                   7293:     }
                   7294:     $courseid=$env{'request.course.id'};
                   7295:     $domain=$env{'user.domain'};
                   7296:     $name=$env{'user.name'};
                   7297:     if ($name eq 'public' && $domain eq 'public') {
                   7298: 	if (!defined($env{'form.username'})) {
                   7299: 	    $env{'form.username'}.=time.rand(10000000);
                   7300: 	}
                   7301: 	$name.=$env{'form.username'};
                   7302:     }
                   7303:     return ($symb,$courseid,$domain,$name,$publicuser);
                   7304: 
                   7305: }
                   7306: 
1.36      albertel 7307: # ------------------------------------------------------------ Serves up a file
1.472     albertel 7308: # returns either the contents of the file or 
                   7309: # -1 if the file doesn't exist
1.481     raeburn  7310: #
                   7311: # if the target is a file that was uploaded via DOCS, 
                   7312: # a check will be made to see if a current copy exists on the local server,
                   7313: # if it does this will be served, otherwise a copy will be retrieved from
                   7314: # the home server for the course and stored in /home/httpd/html/userfiles on
                   7315: # the local server.   
1.472     albertel 7316: 
1.36      albertel 7317: sub getfile {
1.538     albertel 7318:     my ($file) = @_;
1.609     banghart 7319:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 7320:     &repcopy($file);
                   7321:     return &readfile($file);
                   7322: }
                   7323: 
                   7324: sub repcopy_userfile {
                   7325:     my ($file)=@_;
1.609     banghart 7326:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610     albertel 7327:     if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 7328:     my ($cdom,$cnum,$filename) = 
1.811     albertel 7329: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 7330:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   7331:     if (-e "$file") {
1.828     www      7332: # we already have a local copy, check it out
1.538     albertel 7333: 	my @fileinfo = stat($file);
1.828     www      7334: 	my $rtncode;
                   7335: 	my $info;
1.538     albertel 7336: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 7337: 	if ($lwpresp ne 'ok') {
1.828     www      7338: # there is no such file anymore, even though we had a local copy
1.482     albertel 7339: 	    if ($rtncode eq '404') {
1.538     albertel 7340: 		unlink($file);
1.482     albertel 7341: 	    }
                   7342: 	    return -1;
                   7343: 	}
                   7344: 	if ($info < $fileinfo[9]) {
1.828     www      7345: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  7346: 	    return 'ok';
1.828     www      7347: 	} else {
                   7348: # the file is outdated, get rid of it
                   7349: 	    unlink($file);
1.482     albertel 7350: 	}
1.828     www      7351:     }
                   7352: # one way or the other, at this point, we don't have the file
                   7353: # construct the correct path for the file
                   7354:     my @parts = ($cdom,$cnum); 
                   7355:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   7356: 	push @parts, split(/\//,$1);
                   7357:     }
                   7358:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   7359:     foreach my $part (@parts) {
                   7360: 	$path .= '/'.$part;
                   7361: 	if (!-e $path) {
                   7362: 	    mkdir($path,0770);
1.482     albertel 7363: 	}
                   7364:     }
1.828     www      7365: # now the path exists for sure
                   7366: # get a user agent
                   7367:     my $ua=new LWP::UserAgent;
                   7368:     my $transferfile=$file.'.in.transfer';
                   7369: # FIXME: this should flock
                   7370:     if (-e $transferfile) { return 'ok'; }
                   7371:     my $request;
                   7372:     $uri=~s/^\///;
1.838     albertel 7373:     $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri);
1.828     www      7374:     my $response=$ua->request($request,$transferfile);
                   7375: # did it work?
                   7376:     if ($response->is_error()) {
                   7377: 	unlink($transferfile);
                   7378: 	&logthis("Userfile repcopy failed for $uri");
                   7379: 	return -1;
                   7380:     }
                   7381: # worked, rename the transfer file
                   7382:     rename($transferfile,$file);
1.607     raeburn  7383:     return 'ok';
1.481     raeburn  7384: }
                   7385: 
1.517     albertel 7386: sub tokenwrapper {
                   7387:     my $uri=shift;
1.552     albertel 7388:     $uri=~s|^http\://([^/]+)||;
                   7389:     $uri=~s|^/||;
1.620     albertel 7390:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 7391:     my $token=$1;
1.552     albertel 7392:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   7393:     if ($udom && $uname && $file) {
                   7394: 	$file=~s|(\?\.*)*$||;
1.620     albertel 7395:         &appenv("userfile.$udom/$uname/$file" => $env{'request.course.id'});
1.838     albertel 7396:         return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri.
1.517     albertel 7397:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   7398:                                '&tokenissued='.$perlvar{'lonHostID'};
                   7399:     } else {
                   7400:         return '/adm/notfound.html';
                   7401:     }
                   7402: }
                   7403: 
1.828     www      7404: # call with reqtype HEAD: get last modification time
                   7405: # call with reqtype GET: get the file contents
                   7406: # Do not call this with reqtype GET for large files! It loads everything into memory
                   7407: #
1.481     raeburn  7408: sub getuploaded {
                   7409:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   7410:     $uri=~s/^\///;
1.838     albertel 7411:     $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri;
1.481     raeburn  7412:     my $ua=new LWP::UserAgent;
                   7413:     my $request=new HTTP::Request($reqtype,$uri);
                   7414:     my $response=$ua->request($request);
                   7415:     $$rtncode = $response->code;
1.482     albertel 7416:     if (! $response->is_success()) {
                   7417: 	return 'failed';
                   7418:     }      
                   7419:     if ($reqtype eq 'HEAD') {
1.486     www      7420: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 7421:     } elsif ($reqtype eq 'GET') {
                   7422: 	$$info = $response->content;
1.472     albertel 7423:     }
1.482     albertel 7424:     return 'ok';
1.36      albertel 7425: }
                   7426: 
1.481     raeburn  7427: sub readfile {
                   7428:     my $file = shift;
                   7429:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   7430:     my $fh;
                   7431:     open($fh,"<$file");
                   7432:     my $a='';
1.800     albertel 7433:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  7434:     return $a;
                   7435: }
                   7436: 
1.36      albertel 7437: sub filelocation {
1.590     banghart 7438:     my ($dir,$file) = @_;
                   7439:     my $location;
                   7440:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 7441: 
                   7442:     if ($file =~ m-^/adm/-) {
                   7443: 	$file=~s-^/adm/wrapper/-/-;
                   7444: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   7445:     }
1.590     banghart 7446:     if ($file=~m:^/~:) { # is a contruction space reference
                   7447:         $location = $file;
                   7448:         $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807     albertel 7449:     } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649     albertel 7450: 	# is a correct contruction space reference
                   7451:         $location = $file;
1.609     banghart 7452:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 7453:         my ($udom,$uname,$filename)=
1.811     albertel 7454:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 7455:         my $home=&homeserver($uname,$udom);
                   7456:         my $is_me=0;
                   7457:         my @ids=&current_machine_ids();
                   7458:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   7459:         if ($is_me) {
1.740     www      7460:   	    $location=&propath($udom,$uname).
1.590     banghart 7461:   	      '/userfiles/'.$filename;
                   7462:         } else {
                   7463:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   7464:   	      $udom.'/'.$uname.'/'.$filename;
                   7465:         }
                   7466:     } else {
                   7467:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
                   7468:         $file=~s:^/res/:/:;
                   7469:         if ( !( $file =~ m:^/:) ) {
                   7470:             $location = $dir. '/'.$file;
                   7471:         } else {
                   7472:             $location = '/home/httpd/html/res'.$file;
                   7473:         }
1.59      albertel 7474:     }
1.590     banghart 7475:     $location=~s://+:/:g; # remove duplicate /
                   7476:     while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
                   7477:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   7478:     return $location;
1.46      www      7479: }
1.36      albertel 7480: 
1.46      www      7481: sub hreflocation {
                   7482:     my ($dir,$file)=@_;
1.460     albertel 7483:     unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
1.666     albertel 7484: 	$file=filelocation($dir,$file);
1.700     albertel 7485:     } elsif ($file=~m-^/adm/-) {
                   7486: 	$file=~s-^/adm/wrapper/-/-;
                   7487: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 7488:     }
                   7489:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   7490: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807     albertel 7491:     } elsif ($file=~m-/home/($match_username)/public_html/-) {
                   7492: 	$file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666     albertel 7493:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811     albertel 7494: 	$file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666     albertel 7495: 	    -/uploaded/$1/$2/-x;
1.46      www      7496:     }
1.462     albertel 7497:     return $file;
1.465     albertel 7498: }
                   7499: 
                   7500: sub current_machine_domains {
1.853     albertel 7501:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   7502: }
                   7503: 
                   7504: sub machine_domains {
                   7505:     my ($hostname) = @_;
1.465     albertel 7506:     my @domains;
1.838     albertel 7507:     my %hostname = &all_hostnames();
1.465     albertel 7508:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  7509: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 7510: 	if ($hostname eq $name) {
1.844     albertel 7511: 	    push(@domains,&host_domain($id));
1.465     albertel 7512: 	}
                   7513:     }
                   7514:     return @domains;
                   7515: }
                   7516: 
                   7517: sub current_machine_ids {
1.853     albertel 7518:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   7519: }
                   7520: 
                   7521: sub machine_ids {
                   7522:     my ($hostname) = @_;
                   7523:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 7524:     my @ids;
1.838     albertel 7525:     my %hostname = &all_hostnames();
1.465     albertel 7526:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  7527: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 7528: 	if ($hostname eq $name) {
                   7529: 	    push(@ids,$id);
                   7530: 	}
                   7531:     }
                   7532:     return @ids;
1.31      www      7533: }
                   7534: 
1.824     raeburn  7535: sub additional_machine_domains {
                   7536:     my @domains;
                   7537:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
                   7538:     while( my $line = <$fh>) {
                   7539:         $line =~ s/\s//g;
                   7540:         push(@domains,$line);
                   7541:     }
                   7542:     return @domains;
                   7543: }
                   7544: 
                   7545: sub default_login_domain {
                   7546:     my $domain = $perlvar{'lonDefDomain'};
                   7547:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   7548:     foreach my $posdom (&current_machine_domains(),
                   7549:                         &additional_machine_domains()) {
                   7550:         if (lc($posdom) eq lc($testdomain)) {
                   7551:             $domain=$posdom;
                   7552:             last;
                   7553:         }
                   7554:     }
                   7555:     return $domain;
                   7556: }
                   7557: 
1.31      www      7558: # ------------------------------------------------------------- Declutters URLs
                   7559: 
                   7560: sub declutter {
                   7561:     my $thisfn=shift;
1.569     albertel 7562:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479     albertel 7563:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31      www      7564:     $thisfn=~s/^\///;
1.697     albertel 7565:     $thisfn=~s|^adm/wrapper/||;
                   7566:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      7567:     $thisfn=~s/^res\///;
1.235     www      7568:     $thisfn=~s/\?.+$//;
1.268     www      7569:     return $thisfn;
                   7570: }
                   7571: 
                   7572: # ------------------------------------------------------------- Clutter up URLs
                   7573: 
                   7574: sub clutter {
                   7575:     my $thisfn='/'.&declutter(shift);
1.609     banghart 7576:     unless ($thisfn=~/^\/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)\//) { 
1.270     www      7577:        $thisfn='/res'.$thisfn; 
                   7578:     }
1.694     albertel 7579:     if ($thisfn !~m|/adm|) {
1.695     albertel 7580: 	if ($thisfn =~ m|/ext/|) {
1.694     albertel 7581: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 7582: 	} else {
                   7583: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   7584: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 7585: 	    if ($embstyle eq 'ssi'
                   7586: 		|| ($embstyle eq 'hdn')
                   7587: 		|| ($embstyle eq 'rat')
                   7588: 		|| ($embstyle eq 'prv')
                   7589: 		|| ($embstyle eq 'ign')) {
                   7590: 		#do nothing with these
                   7591: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 7592: 		|| ($embstyle eq 'emb')
                   7593: 		|| ($embstyle eq 'wrp')) {
                   7594: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 7595: 	    } elsif ($embstyle eq 'unk'
                   7596: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 7597: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 7598: 	    } else {
1.718     www      7599: #		&logthis("Got a blank emb style");
1.695     albertel 7600: 	    }
1.694     albertel 7601: 	}
                   7602:     }
1.31      www      7603:     return $thisfn;
1.12      www      7604: }
                   7605: 
1.787     albertel 7606: sub clutter_with_no_wrapper {
                   7607:     my $uri = &clutter(shift);
                   7608:     if ($uri =~ m-^/adm/-) {
                   7609: 	$uri =~ s-^/adm/wrapper/-/-;
                   7610: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   7611:     }
                   7612:     return $uri;
                   7613: }
                   7614: 
1.557     albertel 7615: sub freeze_escape {
                   7616:     my ($value)=@_;
                   7617:     if (ref($value)) {
                   7618: 	$value=&nfreeze($value);
                   7619: 	return '__FROZEN__'.&escape($value);
                   7620:     }
                   7621:     return &escape($value);
                   7622: }
                   7623: 
1.11      www      7624: 
1.557     albertel 7625: sub thaw_unescape {
                   7626:     my ($value)=@_;
                   7627:     if ($value =~ /^__FROZEN__/) {
                   7628: 	substr($value,0,10,undef);
                   7629: 	$value=&unescape($value);
                   7630: 	return &thaw($value);
                   7631:     }
                   7632:     return &unescape($value);
                   7633: }
                   7634: 
1.436     albertel 7635: sub correct_line_ends {
                   7636:     my ($result)=@_;
                   7637:     $$result =~s/\r\n/\n/mg;
                   7638:     $$result =~s/\r/\n/mg;
1.415     albertel 7639: }
1.1       albertel 7640: # ================================================================ Main Program
                   7641: 
1.184     www      7642: sub goodbye {
1.204     albertel 7643:    &logthis("Starting Shut down");
1.443     albertel 7644: #not converted to using infrastruture and probably shouldn't be
1.599     albertel 7645:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&freeze(\%badServerCache))));
1.443     albertel 7646: #converted
1.599     albertel 7647: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
                   7648:    &logthis(sprintf("%-20s is %s",'%homecache',length(&freeze(\%homecache))));
                   7649: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&freeze(\%titlecache))));
                   7650: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&freeze(\%courseresdatacache))));
1.425     albertel 7651: #1.1 only
1.599     albertel 7652: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&freeze(\%userresdatacache))));
                   7653: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&freeze(\%getsectioncache))));
                   7654: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&freeze(\%courseresversioncache))));
                   7655: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&freeze(\%resversioncache))));
                   7656:    &logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
                   7657:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   7658:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      7659:    &flushcourselogs();
                   7660:    &logthis("Shutting down");
                   7661: }
                   7662: 
1.852     albertel 7663: sub get_dns {
                   7664:     my ($url,$func) = @_;
                   7665:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   7666:     foreach my $dns (<$config>) {
                   7667: 	next if ($dns !~ /^\^(\S*)/x);
                   7668: 	$dns = $1;
                   7669: 	my $ua=new LWP::UserAgent;
                   7670: 	my $request=new HTTP::Request('GET',"http://$dns$url");
                   7671: 	my $response=$ua->request($request);
                   7672: 	next if ($response->is_error());
                   7673: 	my @content = split("\n",$response->content);
                   7674: 	&$func(\@content);
                   7675:     }
                   7676:     close($config);
                   7677: }
1.327     albertel 7678: # ------------------------------------------------------------ Read domain file
                   7679: {
1.852     albertel 7680:     my $loaded;
1.846     albertel 7681:     my %domain;
                   7682: 
1.852     albertel 7683:     sub parse_domain_tab {
                   7684: 	my ($lines) = @_;
                   7685: 	foreach my $line (@$lines) {
                   7686: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      7687: 
1.846     albertel 7688: 	    chomp($line);
1.852     albertel 7689: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 7690: 	    my %this_domain;
                   7691: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   7692: 			       'lang_def', 'city', 'longi', 'lati',
                   7693: 			       'primary') {
                   7694: 		$this_domain{$field} = shift(@elements);
                   7695: 	    }
                   7696: 	    $domain{$name} = \%this_domain;
1.852     albertel 7697: 	}
                   7698:     }
1.864     albertel 7699: 
                   7700:     sub reset_domain_info {
                   7701: 	undef($loaded);
                   7702: 	undef(%domain);
                   7703:     }
                   7704: 
1.852     albertel 7705:     sub load_domain_tab {
                   7706: 	&get_dns('/adm/dns/domain',\&parse_domain_tab);
                   7707: 	my $fh;
                   7708: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
                   7709: 	    my @lines = <$fh>;
                   7710: 	    &parse_domain_tab(\@lines);
1.448     albertel 7711: 	}
1.852     albertel 7712: 	close($fh);
                   7713: 	$loaded = 1;
1.327     albertel 7714:     }
1.846     albertel 7715: 
                   7716:     sub domain {
1.852     albertel 7717: 	&load_domain_tab() if (!$loaded);
                   7718: 
1.846     albertel 7719: 	my ($name,$what) = @_;
                   7720: 	return if ( !exists($domain{$name}) );
                   7721: 
                   7722: 	if (!$what) {
                   7723: 	    return $domain{$name}{'description'};
                   7724: 	}
                   7725: 	return $domain{$name}{$what};
                   7726:     }
1.327     albertel 7727: }
                   7728: 
                   7729: 
1.1       albertel 7730: # ------------------------------------------------------------- Read hosts file
                   7731: {
1.838     albertel 7732:     my %hostname;
1.844     albertel 7733:     my %hostdom;
1.845     albertel 7734:     my %libserv;
1.852     albertel 7735:     my $loaded;
                   7736: 
                   7737:     sub parse_hosts_tab {
                   7738: 	my ($file) = @_;
                   7739: 	foreach my $configline (@$file) {
                   7740: 	    next if ($configline =~ /^(\#|\s*$ )/x);
                   7741: 	    next if ($configline =~ /^\^/);
                   7742: 	    chomp($configline);
                   7743: 	    my ($id,$domain,$role,$name)=split(/:/,$configline);
                   7744: 	    $name=~s/\s//g;
                   7745: 	    if ($id && $domain && $role && $name) {
                   7746: 		$hostname{$id}=$name;
                   7747: 		$hostdom{$id}=$domain;
                   7748: 		if ($role eq 'library') { $libserv{$id}=$name; }
                   7749: 	    }
                   7750: 	}
                   7751:     }
1.864     albertel 7752:     
                   7753:     sub reset_hosts_info {
                   7754: 	&reset_domain_info();
                   7755: 	&reset_hosts_ip_info();
                   7756: 	undef(%hostname);
                   7757: 	undef(%hostdom);
                   7758: 	undef(%libserv);
                   7759: 	undef($loaded);
                   7760:     }
1.1       albertel 7761: 
1.852     albertel 7762:     sub load_hosts_tab {
                   7763: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab);
                   7764: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   7765: 	my @config = <$config>;
                   7766: 	&parse_hosts_tab(\@config);
                   7767: 	close($config);
                   7768: 	$loaded=1;
1.1       albertel 7769:     }
1.852     albertel 7770: 
1.838     albertel 7771:     sub hostname {
1.852     albertel 7772: 	&load_hosts_tab() if (!$loaded);
                   7773: 
1.838     albertel 7774: 	my ($lonid) = @_;
                   7775: 	return $hostname{$lonid};
                   7776:     }
1.845     albertel 7777: 
1.838     albertel 7778:     sub all_hostnames {
1.852     albertel 7779: 	&load_hosts_tab() if (!$loaded);
                   7780: 
1.838     albertel 7781: 	return %hostname;
                   7782:     }
1.845     albertel 7783: 
                   7784:     sub is_library {
1.852     albertel 7785: 	&load_hosts_tab() if (!$loaded);
                   7786: 
1.845     albertel 7787: 	return exists($libserv{$_[0]});
                   7788:     }
                   7789: 
                   7790:     sub all_library {
1.852     albertel 7791: 	&load_hosts_tab() if (!$loaded);
                   7792: 
1.845     albertel 7793: 	return %libserv;
                   7794:     }
                   7795: 
1.841     albertel 7796:     sub get_servers {
1.852     albertel 7797: 	&load_hosts_tab() if (!$loaded);
                   7798: 
1.841     albertel 7799: 	my ($domain,$type) = @_;
                   7800: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   7801: 	                                          : %hostname;
                   7802: 	my %result;
1.842     albertel 7803: 	if (ref($domain) eq 'ARRAY') {
                   7804: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 7805: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 7806: 		    $result{$host} = $hostname;
                   7807: 		}
                   7808: 	    }
                   7809: 	} else {
                   7810: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   7811: 		if ($hostdom{$host} eq $domain) {
                   7812: 		    $result{$host} = $hostname;
                   7813: 		}
1.841     albertel 7814: 	    }
                   7815: 	}
                   7816: 	return %result;
                   7817:     }
1.845     albertel 7818: 
1.844     albertel 7819:     sub host_domain {
1.852     albertel 7820: 	&load_hosts_tab() if (!$loaded);
                   7821: 
1.844     albertel 7822: 	my ($lonid) = @_;
                   7823: 	return $hostdom{$lonid};
                   7824:     }
                   7825: 
1.841     albertel 7826:     sub all_domains {
1.852     albertel 7827: 	&load_hosts_tab() if (!$loaded);
                   7828: 
1.841     albertel 7829: 	my %seen;
                   7830: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   7831: 	return @uniq;
                   7832:     }
1.1       albertel 7833: }
                   7834: 
1.847     albertel 7835: { 
                   7836:     my %iphost;
1.856     albertel 7837:     my %name_to_ip;
                   7838:     my %lonid_to_ip;
1.847     albertel 7839:     sub get_hosts_from_ip {
                   7840: 	my ($ip) = @_;
                   7841: 	my %iphosts = &get_iphost();
                   7842: 	if (ref($iphosts{$ip})) {
                   7843: 	    return @{$iphosts{$ip}};
                   7844: 	}
                   7845: 	return;
1.839     albertel 7846:     }
1.864     albertel 7847:     
                   7848:     sub reset_hosts_ip_info {
                   7849: 	undef(%iphost);
                   7850: 	undef(%name_to_ip);
                   7851: 	undef(%lonid_to_ip);
                   7852:     }
1.856     albertel 7853: 
                   7854:     sub get_host_ip {
                   7855: 	my ($lonid) = @_;
                   7856: 	if (exists($lonid_to_ip{$lonid})) {
                   7857: 	    return $lonid_to_ip{$lonid};
                   7858: 	}
                   7859: 	my $name=&hostname($lonid);
                   7860:    	my $ip = gethostbyname($name);
                   7861: 	return if (!$ip || length($ip) ne 4);
                   7862: 	$ip=inet_ntoa($ip);
                   7863: 	$name_to_ip{$name}   = $ip;
                   7864: 	$lonid_to_ip{$lonid} = $ip;
                   7865: 	return $ip;
                   7866:     }
1.847     albertel 7867:     
                   7868:     sub get_iphost {
                   7869: 	if (%iphost) { return %iphost; }
                   7870: 	my %hostname = &all_hostnames();
                   7871: 	foreach my $id (keys(%hostname)) {
1.864     albertel 7872: 	    my $name=&hostname($id);
1.847     albertel 7873: 	    my $ip;
                   7874: 	    if (!exists($name_to_ip{$name})) {
                   7875: 		$ip = gethostbyname($name);
                   7876: 		if (!$ip || length($ip) ne 4) {
                   7877: 		    &logthis("Skipping host $id name $name no IP found");
                   7878: 		    next;
                   7879: 		}
                   7880: 		$ip=inet_ntoa($ip);
                   7881: 		$name_to_ip{$name} = $ip;
                   7882: 	    } else {
                   7883: 		$ip = $name_to_ip{$name};
1.653     albertel 7884: 	    }
1.856     albertel 7885: 	    $lonid_to_ip{$id} = $ip;
1.847     albertel 7886: 	    push(@{$iphost{$ip}},$id);
1.598     albertel 7887: 	}
1.847     albertel 7888: 	return %iphost;
1.598     albertel 7889:     }
                   7890: }
                   7891: 
1.862     albertel 7892: BEGIN {
                   7893: 
                   7894: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   7895:     unless ($readit) {
                   7896: {
                   7897:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   7898:     %perlvar = (%perlvar,%{$configvars});
                   7899: }
                   7900: 
                   7901: 
1.1       albertel 7902: # ------------------------------------------------------ Read spare server file
                   7903: {
1.448     albertel 7904:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 7905: 
                   7906:     while (my $configline=<$config>) {
                   7907:        chomp($configline);
1.284     matthew  7908:        if ($configline) {
1.784     albertel 7909: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 7910: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 7911: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 7912:        }
                   7913:     }
1.448     albertel 7914:     close($config);
1.1       albertel 7915: }
1.11      www      7916: # ------------------------------------------------------------ Read permissions
                   7917: {
1.448     albertel 7918:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11      www      7919: 
                   7920:     while (my $configline=<$config>) {
1.448     albertel 7921: 	chomp($configline);
                   7922: 	if ($configline) {
                   7923: 	    my ($role,$perm)=split(/ /,$configline);
                   7924: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   7925: 	}
1.11      www      7926:     }
1.448     albertel 7927:     close($config);
1.11      www      7928: }
                   7929: 
                   7930: # -------------------------------------------- Read plain texts for permissions
                   7931: {
1.448     albertel 7932:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      7933: 
                   7934:     while (my $configline=<$config>) {
1.448     albertel 7935: 	chomp($configline);
                   7936: 	if ($configline) {
1.742     raeburn  7937: 	    my ($short,@plain)=split(/:/,$configline);
                   7938:             %{$prp{$short}} = ();
                   7939: 	    if (@plain > 0) {
                   7940:                 $prp{$short}{'std'} = $plain[0];
                   7941:                 for (my $i=1; $i<@plain; $i++) {
                   7942:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   7943:                 }
                   7944:             }
1.448     albertel 7945: 	}
1.135     www      7946:     }
1.448     albertel 7947:     close($config);
1.135     www      7948: }
                   7949: 
                   7950: # ---------------------------------------------------------- Read package table
                   7951: {
1.448     albertel 7952:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135     www      7953: 
                   7954:     while (my $configline=<$config>) {
1.483     albertel 7955: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 7956: 	chomp($configline);
                   7957: 	my ($short,$plain)=split(/:/,$configline);
                   7958: 	my ($pack,$name)=split(/\&/,$short);
                   7959: 	if ($plain ne '') {
                   7960: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   7961: 	    $packagetab{$short}=$plain; 
                   7962: 	}
1.11      www      7963:     }
1.448     albertel 7964:     close($config);
1.329     matthew  7965: }
                   7966: 
                   7967: # ------------- set up temporary directory
                   7968: {
                   7969:     $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
                   7970: 
1.11      www      7971: }
                   7972: 
1.794     albertel 7973: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   7974: 				'compress_threshold'=> 20_000,
                   7975:  			        });
1.185     www      7976: 
1.281     www      7977: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      7978: $dumpcount=0;
1.22      www      7979: 
1.163     harris41 7980: &logtouch();
1.672     albertel 7981: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      7982: $readit=1;
1.564     albertel 7983:     {
                   7984: 	use integer;
                   7985: 	my $test=(2**32)+1;
1.568     albertel 7986: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 7987: 	&logthis(" Detected 64bit platform ($_64bit)");
                   7988:     }
1.195     www      7989: }
1.1       albertel 7990: }
1.179     www      7991: 
1.1       albertel 7992: 1;
1.191     harris41 7993: __END__
                   7994: 
1.243     albertel 7995: =pod
                   7996: 
1.191     harris41 7997: =head1 NAME
                   7998: 
1.243     albertel 7999: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 8000: 
                   8001: =head1 SYNOPSIS
                   8002: 
1.243     albertel 8003: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 8004: 
                   8005:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   8006: 
1.243     albertel 8007: Common parameters:
                   8008: 
                   8009: =over 4
                   8010: 
                   8011: =item *
                   8012: 
                   8013: $uname : an internal username (if $cname expecting a course Id specifically)
                   8014: 
                   8015: =item *
                   8016: 
                   8017: $udom : a domain (if $cdom expecting a course's domain specifically)
                   8018: 
                   8019: =item *
                   8020: 
                   8021: $symb : a resource instance identifier
                   8022: 
                   8023: =item *
                   8024: 
                   8025: $namespace : the name of a .db file that contains the data needed or
                   8026: being set.
                   8027: 
                   8028: =back
                   8029: 
1.394     bowersj2 8030: =head1 OVERVIEW
1.191     harris41 8031: 
1.394     bowersj2 8032: lonnet provides subroutines which interact with the
                   8033: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   8034: about classes, users, and resources.
1.243     albertel 8035: 
                   8036: For many of these objects you can also use this to store data about
                   8037: them or modify them in various ways.
1.191     harris41 8038: 
1.394     bowersj2 8039: =head2 Symbs
1.191     harris41 8040: 
1.394     bowersj2 8041: To identify a specific instance of a resource, LON-CAPA uses symbols
                   8042: or "symbs"X<symb>. These identifiers are built from the URL of the
                   8043: map, the resource number of the resource in the map, and the URL of
                   8044: the resource itself. The latter is somewhat redundant, but might help
                   8045: if maps change.
                   8046: 
                   8047: An example is
                   8048: 
                   8049:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   8050: 
                   8051: The respective map entry is
                   8052: 
                   8053:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   8054:   title="Problem 2">
                   8055:  </resource>
                   8056: 
                   8057: Symbs are used by the random number generator, as well as to store and
                   8058: restore data specific to a certain instance of for example a problem.
                   8059: 
                   8060: =head2 Storing And Retrieving Data
                   8061: 
                   8062: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   8063: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   8064: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   8065: is is the non-critical message twin of cstore. These functions are for
                   8066: handlers to store a perl hash to a user's permanent data space in an
                   8067: easy manner, and to retrieve it again on another call. It is expected
                   8068: that a handler would use this once at the beginning to retrieve data,
                   8069: and then again once at the end to send only the new data back.
                   8070: 
                   8071: The data is stored in the user's data directory on the user's
                   8072: homeserver under the ID of the course.
                   8073: 
                   8074: The hash that is returned by restore will have all of the previous
                   8075: value for all of the elements of the hash.
                   8076: 
                   8077: Example:
                   8078: 
                   8079:  #creating a hash
                   8080:  my %hash;
                   8081:  $hash{'foo'}='bar';
                   8082: 
                   8083:  #storing it
                   8084:  &Apache::lonnet::cstore(\%hash);
                   8085: 
                   8086:  #changing a value
                   8087:  $hash{'foo'}='notbar';
                   8088: 
                   8089:  #adding a new value
                   8090:  $hash{'bar'}='foo';
                   8091:  &Apache::lonnet::cstore(\%hash);
                   8092: 
                   8093:  #retrieving the hash
                   8094:  my %history=&Apache::lonnet::restore();
                   8095: 
                   8096:  #print the hash
                   8097:  foreach my $key (sort(keys(%history))) {
                   8098:    print("\%history{$key} = $history{$key}");
                   8099:  }
                   8100: 
                   8101: Will print out:
1.191     harris41 8102: 
1.394     bowersj2 8103:  %history{1:foo} = bar
                   8104:  %history{1:keys} = foo:timestamp
                   8105:  %history{1:timestamp} = 990455579
                   8106:  %history{2:bar} = foo
                   8107:  %history{2:foo} = notbar
                   8108:  %history{2:keys} = foo:bar:timestamp
                   8109:  %history{2:timestamp} = 990455580
                   8110:  %history{bar} = foo
                   8111:  %history{foo} = notbar
                   8112:  %history{timestamp} = 990455580
                   8113:  %history{version} = 2
                   8114: 
                   8115: Note that the special hash entries C<keys>, C<version> and
                   8116: C<timestamp> were added to the hash. C<version> will be equal to the
                   8117: total number of versions of the data that have been stored. The
                   8118: C<timestamp> attribute will be the UNIX time the hash was
                   8119: stored. C<keys> is available in every historical section to list which
                   8120: keys were added or changed at a specific historical revision of a
                   8121: hash.
                   8122: 
                   8123: B<Warning>: do not store the hash that restore returns directly. This
                   8124: will cause a mess since it will restore the historical keys as if the
                   8125: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 8126: 
1.394     bowersj2 8127: Calling convention:
1.191     harris41 8128: 
1.394     bowersj2 8129:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
                   8130:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191     harris41 8131: 
1.394     bowersj2 8132: For more detailed information, see lonnet specific documentation.
1.191     harris41 8133: 
1.394     bowersj2 8134: =head1 RETURN MESSAGES
1.191     harris41 8135: 
1.394     bowersj2 8136: =over 4
1.191     harris41 8137: 
1.394     bowersj2 8138: =item * B<con_lost>: unable to contact remote host
1.191     harris41 8139: 
1.394     bowersj2 8140: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   8141: when the connection is brought back up
1.191     harris41 8142: 
1.394     bowersj2 8143: =item * B<con_failed>: unable to contact remote host and unable to save message
                   8144: for later delivery
1.191     harris41 8145: 
1.394     bowersj2 8146: =item * B<error:>: an error a occured, a description of the error follows the :
1.191     harris41 8147: 
1.394     bowersj2 8148: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 8149: that was requested
1.191     harris41 8150: 
1.243     albertel 8151: =back
1.191     harris41 8152: 
1.243     albertel 8153: =head1 PUBLIC SUBROUTINES
1.191     harris41 8154: 
1.243     albertel 8155: =head2 Session Environment Functions
1.191     harris41 8156: 
1.243     albertel 8157: =over 4
1.191     harris41 8158: 
1.394     bowersj2 8159: =item * 
                   8160: X<appenv()>
                   8161: B<appenv(%hash)>: the value of %hash is written to
                   8162: the user envirnoment file, and will be restored for each access this
1.620     albertel 8163: user makes during this session, also modifies the %env for the current
1.394     bowersj2 8164: process
1.191     harris41 8165: 
                   8166: =item *
1.394     bowersj2 8167: X<delenv()>
                   8168: B<delenv($regexp)>: removes all items from the session
                   8169: environment file that matches the regular expression in $regexp. The
1.620     albertel 8170: values are also delted from the current processes %env.
1.191     harris41 8171: 
1.795     albertel 8172: =item * get_env_multiple($name) 
                   8173: 
                   8174: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   8175: values may be defined and end up as an array ref.
                   8176: 
                   8177: returns an array of values
                   8178: 
1.243     albertel 8179: =back
                   8180: 
                   8181: =head2 User Information
1.191     harris41 8182: 
1.243     albertel 8183: =over 4
1.191     harris41 8184: 
                   8185: =item *
1.394     bowersj2 8186: X<queryauthenticate()>
                   8187: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 8188: authentication scheme
                   8189: 
                   8190: =item *
1.394     bowersj2 8191: X<authenticate()>
                   8192: B<authenticate($uname,$upass,$udom)>: try to
                   8193: authenticate user from domain's lib servers (first use the current
                   8194: one). C<$upass> should be the users password.
1.191     harris41 8195: 
                   8196: =item *
1.394     bowersj2 8197: X<homeserver()>
                   8198: B<homeserver($uname,$udom)>: find the server which has
                   8199: the user's directory and files (there must be only one), this caches
                   8200: the answer, and also caches if there is a borken connection.
1.191     harris41 8201: 
                   8202: =item *
1.394     bowersj2 8203: X<idget()>
                   8204: B<idget($udom,@ids)>: find the usernames behind a list of IDs
                   8205: (IDs are a unique resource in a domain, there must be only 1 ID per
                   8206: username, and only 1 username per ID in a specific domain) (returns
                   8207: hash: id=>name,id=>name)
1.191     harris41 8208: 
                   8209: =item *
1.394     bowersj2 8210: X<idrget()>
                   8211: B<idrget($udom,@unames)>: find the IDs behind a list of
                   8212: usernames (returns hash: name=>id,name=>id)
1.191     harris41 8213: 
                   8214: =item *
1.394     bowersj2 8215: X<idput()>
                   8216: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191     harris41 8217: 
                   8218: =item *
1.394     bowersj2 8219: X<rolesinit()>
                   8220: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243     albertel 8221: 
                   8222: =item *
1.551     albertel 8223: X<getsection()>
                   8224: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 8225: course $cname, return section name/number or '' for "not in course"
                   8226: and '-1' for "no section"
                   8227: 
                   8228: =item *
1.394     bowersj2 8229: X<userenvironment()>
                   8230: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 8231: passed in @what from the requested user's environment, returns a hash
                   8232: 
1.858     raeburn  8233: =item * 
                   8234: X<userlog_query()>
1.859     albertel 8235: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   8236: activity.log file. %filters defines filters applied when parsing the
                   8237: log file. These can be start or end timestamps, or the type of action
                   8238: - log to look for Login or Logout events, check for Checkin or
                   8239: Checkout, role for role selection. The response is in the form
                   8240: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   8241: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  8242: 
1.243     albertel 8243: =back
                   8244: 
                   8245: =head2 User Roles
                   8246: 
                   8247: =over 4
                   8248: 
                   8249: =item *
                   8250: 
1.810     raeburn  8251: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243     albertel 8252:  F: full access
                   8253:  U,I,K: authentication modes (cxx only)
                   8254:  '': forbidden
                   8255:  1: user needs to choose course
                   8256:  2: browse allowed
1.766     albertel 8257:  A: passphrase authentication needed
1.243     albertel 8258: 
                   8259: =item *
                   8260: 
                   8261: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
                   8262: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
                   8263: and course level
                   8264: 
                   8265: =item *
                   8266: 
                   8267: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
                   8268: explanation of a user role term
                   8269: 
1.832     raeburn  8270: =item *
                   8271: 
1.858     raeburn  8272: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms) :
                   8273: All arguments are optional. Returns a hash of a roles, either for
                   8274: co-author/assistant author roles for a user's Construction Space
                   8275: (default), or if $context is 'user', roles for the user himself,
                   8276: In the hash, keys are set to colon-sparated $uname,$udom,and $role,
                   8277: and value is set to colon-separated start and end times for the role.
                   8278: If no username and domain are specified, will default to current
                   8279: user/domain. Types, roles, and roledoms are references to arrays,
                   8280: of role statuses (active, future or previous), roles 
                   8281: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   8282: to restrict the list of roles reported. If no array ref is 
                   8283: provided for types, will default to return only active roles.
1.834     albertel 8284: 
1.243     albertel 8285: =back
                   8286: 
                   8287: =head2 User Modification
                   8288: 
                   8289: =over 4
                   8290: 
                   8291: =item *
                   8292: 
                   8293: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
                   8294: user for the level given by URL.  Optional start and end dates (leave empty
                   8295: string or zero for "no date")
1.191     harris41 8296: 
                   8297: =item *
                   8298: 
1.243     albertel 8299: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   8300: change a users, password, possible return values are: ok,
                   8301: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   8302: refused
1.191     harris41 8303: 
                   8304: =item *
                   8305: 
1.243     albertel 8306: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 8307: 
                   8308: =item *
                   8309: 
1.243     albertel 8310: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) : 
                   8311: modify user
1.191     harris41 8312: 
                   8313: =item *
                   8314: 
1.286     matthew  8315: modifystudent
                   8316: 
                   8317: modify a students enrollment and identification information.
                   8318: The course id is resolved based on the current users environment.  
                   8319: This means the envoking user must be a course coordinator or otherwise
                   8320: associated with a course.
                   8321: 
1.297     matthew  8322: This call is essentially a wrapper for lonnet::modifyuser and
                   8323: lonnet::modify_student_enrollment
1.286     matthew  8324: 
                   8325: Inputs: 
                   8326: 
                   8327: =over 4
                   8328: 
                   8329: =item B<$udom> Students loncapa domain
                   8330: 
                   8331: =item B<$uname> Students loncapa login name
                   8332: 
                   8333: =item B<$uid> Students id/student number
                   8334: 
                   8335: =item B<$umode> Students authentication mode
                   8336: 
                   8337: =item B<$upass> Students password
                   8338: 
                   8339: =item B<$first> Students first name
                   8340: 
                   8341: =item B<$middle> Students middle name
                   8342: 
                   8343: =item B<$last> Students last name
                   8344: 
                   8345: =item B<$gene> Students generation
                   8346: 
                   8347: =item B<$usec> Students section in course
                   8348: 
                   8349: =item B<$end> Unix time of the roles expiration
                   8350: 
                   8351: =item B<$start> Unix time of the roles start date
                   8352: 
                   8353: =item B<$forceid> If defined, allow $uid to be changed
                   8354: 
                   8355: =item B<$desiredhome> server to use as home server for student
                   8356: 
                   8357: =back
1.297     matthew  8358: 
                   8359: =item *
                   8360: 
                   8361: modify_student_enrollment
                   8362: 
                   8363: Change a students enrollment status in a class.  The environment variable
                   8364: 'role.request.course' must be defined for this function to proceed.
                   8365: 
                   8366: Inputs:
                   8367: 
                   8368: =over 4
                   8369: 
                   8370: =item $udom, students domain
                   8371: 
                   8372: =item $uname, students name
                   8373: 
                   8374: =item $uid, students user id
                   8375: 
                   8376: =item $first, students first name
                   8377: 
                   8378: =item $middle
                   8379: 
                   8380: =item $last
                   8381: 
                   8382: =item $gene
                   8383: 
                   8384: =item $usec
                   8385: 
                   8386: =item $end
                   8387: 
                   8388: =item $start
                   8389: 
                   8390: =back
                   8391: 
1.191     harris41 8392: 
                   8393: =item *
                   8394: 
1.243     albertel 8395: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   8396: custom role; give a custom role to a user for the level given by URL.  Specify
                   8397: name and domain of role author, and role name
1.191     harris41 8398: 
                   8399: =item *
                   8400: 
1.243     albertel 8401: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 8402: 
                   8403: =item *
                   8404: 
1.243     albertel 8405: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   8406: 
                   8407: =back
                   8408: 
                   8409: =head2 Course Infomation
                   8410: 
                   8411: =over 4
1.191     harris41 8412: 
                   8413: =item *
                   8414: 
1.631     albertel 8415: coursedescription($courseid) : returns a hash of information about the
                   8416: specified course id, including all environment settings for the
                   8417: course, the description of the course will be in the hash under the
                   8418: key 'description'
1.191     harris41 8419: 
                   8420: =item *
                   8421: 
1.624     albertel 8422: resdata($name,$domain,$type,@which) : request for current parameter
                   8423: setting for a specific $type, where $type is either 'course' or 'user',
                   8424: @what should be a list of parameters to ask about. This routine caches
                   8425: answers for 5 minutes.
1.243     albertel 8426: 
                   8427: =back
                   8428: 
                   8429: =head2 Course Modification
                   8430: 
                   8431: =over 4
1.191     harris41 8432: 
                   8433: =item *
                   8434: 
1.243     albertel 8435: writecoursepref($courseid,%prefs) : write preferences (environment
                   8436: database) for a course
1.191     harris41 8437: 
                   8438: =item *
                   8439: 
1.243     albertel 8440: createcourse($udom,$description,$url) : make/modify course
                   8441: 
                   8442: =back
                   8443: 
                   8444: =head2 Resource Subroutines
                   8445: 
                   8446: =over 4
1.191     harris41 8447: 
                   8448: =item *
                   8449: 
1.243     albertel 8450: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 8451: 
                   8452: =item *
                   8453: 
1.243     albertel 8454: repcopy($filename) : subscribes to the requested file, and attempts to
                   8455: replicate from the owning library server, Might return
1.607     raeburn  8456: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   8457: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 8458: resource. Expects the local filesystem pathname
                   8459: (/home/httpd/html/res/....)
                   8460: 
                   8461: =back
                   8462: 
                   8463: =head2 Resource Information
                   8464: 
                   8465: =over 4
1.191     harris41 8466: 
                   8467: =item *
                   8468: 
1.243     albertel 8469: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
                   8470: a vairety of different possible values, $varname should be a request
                   8471: string, and the other parameters can be used to specify who and what
                   8472: one is asking about.
                   8473: 
                   8474: Possible values for $varname are environment.lastname (or other item
                   8475: from the envirnment hash), user.name (or someother aspect about the
                   8476: user), resource.0.maxtries (or some other part and parameter of a
                   8477: resource)
1.204     albertel 8478: 
                   8479: =item *
                   8480: 
1.243     albertel 8481: directcondval($number) : get current value of a condition; reads from a state
                   8482: string
1.204     albertel 8483: 
                   8484: =item *
                   8485: 
1.243     albertel 8486: condval($condidx) : value of condition index based on state
1.204     albertel 8487: 
                   8488: =item *
                   8489: 
1.243     albertel 8490: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   8491: resource's metadata, $what should be either a specific key, or either
                   8492: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   8493: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   8494: 
                   8495: this function automatically caches all requests
1.191     harris41 8496: 
                   8497: =item *
                   8498: 
1.243     albertel 8499: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   8500: network of library servers; returns file handle of where SQL and regex results
                   8501: will be stored for query
1.191     harris41 8502: 
                   8503: =item *
                   8504: 
1.243     albertel 8505: symbread($filename) : return symbolic list entry (filename argument optional);
                   8506: returns the data handle
1.191     harris41 8507: 
                   8508: =item *
                   8509: 
1.243     albertel 8510: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582     albertel 8511: a possible symb for the URL in $thisfn, and if is an encryypted
                   8512: resource that the user accessed using /enc/ returns a 1 on success, 0
                   8513: on failure, user must be in a course, as it assumes the existance of
1.620     albertel 8514: the course initial hash, and uses $env('request.course.id'}
1.243     albertel 8515: 
1.191     harris41 8516: 
                   8517: =item *
                   8518: 
1.243     albertel 8519: symbclean($symb) : removes versions numbers from a symb, returns the
                   8520: cleaned symb
1.191     harris41 8521: 
                   8522: =item *
                   8523: 
1.243     albertel 8524: is_on_map($uri) : checks if the $uri is somewhere on the current
                   8525: course map, user must be in a course for it to work.
1.191     harris41 8526: 
                   8527: =item *
                   8528: 
1.243     albertel 8529: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 8530: 
                   8531: =item *
                   8532: 
1.243     albertel 8533: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   8534: a random seed, all arguments are optional, if they aren't sent it uses the
                   8535: environment to derive them. Note: if symb isn't sent and it can't get one
                   8536: from &symbread it will use the current time as its return value
1.191     harris41 8537: 
                   8538: =item *
                   8539: 
1.243     albertel 8540: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   8541: unfakeable, receipt
1.191     harris41 8542: 
                   8543: =item *
                   8544: 
1.620     albertel 8545: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 8546: 
                   8547: =item *
                   8548: 
1.243     albertel 8549: countacc($url) : count the number of accesses to a given URL
1.191     harris41 8550: 
                   8551: =item *
                   8552: 
1.243     albertel 8553: 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 8554: 
                   8555: =item *
                   8556: 
1.243     albertel 8557: 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 8558: 
                   8559: =item *
                   8560: 
1.243     albertel 8561: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 8562: 
                   8563: =item *
                   8564: 
1.243     albertel 8565: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   8566: forcing spreadsheet to reevaluate the resource scores next time.
                   8567: 
                   8568: =back
                   8569: 
                   8570: =head2 Storing/Retreiving Data
                   8571: 
                   8572: =over 4
1.191     harris41 8573: 
                   8574: =item *
                   8575: 
1.243     albertel 8576: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
                   8577: for this url; hashref needs to be given and should be a \%hashname; the
                   8578: remaining args aren't required and if they aren't passed or are '' they will
1.620     albertel 8579: be derived from the env
1.191     harris41 8580: 
                   8581: =item *
                   8582: 
1.243     albertel 8583: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
                   8584: uses critical subroutine
1.191     harris41 8585: 
                   8586: =item *
                   8587: 
1.243     albertel 8588: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   8589: all args are optional
1.191     harris41 8590: 
                   8591: =item *
                   8592: 
1.717     albertel 8593: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   8594: dumps the complete (or key matching regexp) namespace into a hash
                   8595: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   8596: normally &store()ed into
                   8597: 
                   8598: $range should be either an integer '100' (give me the first 100
                   8599:                                            matching records)
                   8600:               or be  two integers sperated by a - with no spaces
                   8601:                  '30-50' (give me the 30th through the 50th matching
                   8602:                           records)
                   8603: 
                   8604: 
                   8605: =item *
                   8606: 
                   8607: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
                   8608: replaces a &store() version of data with a replacement set of data
                   8609: for a particular resource in a namespace passed in the $storehash hash 
                   8610: reference
                   8611: 
                   8612: =item *
                   8613: 
1.243     albertel 8614: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   8615: works very similar to store/cstore, but all data is stored in a
                   8616: temporary location and can be reset using tmpreset, $storehash should
                   8617: be a hash reference, returns nothing on success
1.191     harris41 8618: 
                   8619: =item *
                   8620: 
1.243     albertel 8621: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   8622: similar to restore, but all data is stored in a temporary location and
                   8623: can be reset using tmpreset. Returns a hash of values on success,
                   8624: error string otherwise.
1.191     harris41 8625: 
                   8626: =item *
                   8627: 
1.243     albertel 8628: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   8629: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 8630: 
                   8631: =item *
                   8632: 
1.243     albertel 8633: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   8634: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 8635: 
                   8636: =item *
                   8637: 
1.243     albertel 8638: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   8639: namesp ($udom and $uname are optional)
1.191     harris41 8640: 
                   8641: =item *
                   8642: 
1.702     albertel 8643: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 8644: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 8645: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  8646: 
1.702     albertel 8647: $range should be either an integer '100' (give me the first 100
                   8648:                                            matching records)
                   8649:               or be  two integers sperated by a - with no spaces
                   8650:                  '30-50' (give me the 30th through the 50th matching
                   8651:                           records)
1.449     matthew  8652: =item *
                   8653: 
                   8654: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   8655: $store can be a scalar, an array reference, or if the amount to be 
                   8656: incremented is > 1, a hash reference.
                   8657: 
                   8658: ($udom and $uname are optional)
1.191     harris41 8659: 
                   8660: =item *
                   8661: 
1.243     albertel 8662: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   8663: ($udom and $uname are optional)
1.191     harris41 8664: 
                   8665: =item *
                   8666: 
1.243     albertel 8667: cput($namespace,$storehash,$udom,$uname) : critical put
                   8668: ($udom and $uname are optional)
1.191     harris41 8669: 
                   8670: =item *
                   8671: 
1.748     albertel 8672: newput($namespace,$storehash,$udom,$uname) :
                   8673: 
                   8674: Attempts to store the items in the $storehash, but only if they don't
                   8675: currently exist, if this succeeds you can be certain that you have 
                   8676: successfully created a new key value pair in the $namespace db.
                   8677: 
                   8678: 
                   8679: Args:
                   8680:  $namespace: name of database to store values to
                   8681:  $storehash: hashref to store to the db
                   8682:  $udom: (optional) domain of user containing the db
                   8683:  $uname: (optional) name of user caontaining the db
                   8684: 
                   8685: Returns:
                   8686:  'ok' -> succeeded in storing all keys of $storehash
                   8687:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   8688:                         least <key> already existed in the db (other
                   8689:                         requested keys may also already exist)
                   8690:  'error: <msg>' -> unable to tie the DB or other erorr occured
                   8691:  'con_lost' -> unable to contact request server
                   8692:  'refused' -> action was not allowed by remote machine
                   8693: 
                   8694: 
                   8695: =item *
                   8696: 
1.243     albertel 8697: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   8698: reference filled in from namesp (encrypts the return communication)
                   8699: ($udom and $uname are optional)
1.191     harris41 8700: 
                   8701: =item *
                   8702: 
1.243     albertel 8703: log($udom,$name,$home,$message) : write to permanent log for user; use
                   8704: critical subroutine
                   8705: 
1.806     raeburn  8706: =item *
                   8707: 
1.860     raeburn  8708: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   8709: array reference filled in from namespace found in domain level on either
                   8710: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  8711: 
                   8712: =item *
                   8713: 
1.860     raeburn  8714: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   8715: domain level either on specified domain server ($uhome) or primary domain 
                   8716: server ($udom and $uhome are optional)
1.806     raeburn  8717: 
1.243     albertel 8718: =back
                   8719: 
                   8720: =head2 Network Status Functions
                   8721: 
                   8722: =over 4
1.191     harris41 8723: 
                   8724: =item *
                   8725: 
                   8726: dirlist($uri) : return directory list based on URI
                   8727: 
                   8728: =item *
                   8729: 
1.243     albertel 8730: spareserver() : find server with least workload from spare.tab
                   8731: 
                   8732: =back
                   8733: 
                   8734: =head2 Apache Request
                   8735: 
                   8736: =over 4
1.191     harris41 8737: 
                   8738: =item *
                   8739: 
1.243     albertel 8740: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   8741: localhost, posts hash
                   8742: 
                   8743: =back
                   8744: 
                   8745: =head2 Data to String to Data
                   8746: 
                   8747: =over 4
1.191     harris41 8748: 
                   8749: =item *
                   8750: 
1.243     albertel 8751: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   8752: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 8753: 
                   8754: =item *
                   8755: 
1.243     albertel 8756: hashref2str($hashref) : convert a hashref into a string complete with
                   8757: escaping and '=' and '&' separators, supports elements that are
                   8758: arrayrefs and hashrefs
1.191     harris41 8759: 
                   8760: =item *
                   8761: 
1.243     albertel 8762: arrayref2str($arrayref) : convert an arrayref into a string complete
                   8763: with escaping and '&' separators, supports elements that are arrayrefs
                   8764: and hashrefs
1.191     harris41 8765: 
                   8766: =item *
                   8767: 
1.243     albertel 8768: str2hash($string) : convert string to hash using unescaping and
                   8769: splitting on '=' and '&', supports elements that are arrayrefs and
                   8770: hashrefs
1.191     harris41 8771: 
                   8772: =item *
                   8773: 
1.243     albertel 8774: str2array($string) : convert string to hash using unescaping and
                   8775: splitting on '&', supports elements that are arrayrefs and hashrefs
                   8776: 
                   8777: =back
                   8778: 
                   8779: =head2 Logging Routines
                   8780: 
                   8781: =over 4
                   8782: 
                   8783: These routines allow one to make log messages in the lonnet.log and
                   8784: lonnet.perm logfiles.
1.191     harris41 8785: 
                   8786: =item *
                   8787: 
1.243     albertel 8788: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 8789: 
                   8790: =item *
                   8791: 
1.243     albertel 8792: logthis() : append message to the normal lonnet.log file, it gets
                   8793: preiodically rolled over and deleted.
1.191     harris41 8794: 
                   8795: =item *
                   8796: 
1.243     albertel 8797: logperm() : append a permanent message to lonnet.perm.log, this log
                   8798: file never gets deleted by any automated portion of the system, only
                   8799: messages of critical importance should go in here.
                   8800: 
                   8801: =back
                   8802: 
                   8803: =head2 General File Helper Routines
                   8804: 
                   8805: =over 4
1.191     harris41 8806: 
                   8807: =item *
                   8808: 
1.481     raeburn  8809: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   8810: (a) files in /uploaded
                   8811:   (i) If a local copy of the file exists - 
                   8812:       compares modification date of local copy with last-modified date for 
                   8813:       definitive version stored on home server for course. If local copy is 
                   8814:       stale, requests a new version from the home server and stores it. 
                   8815:       If the original has been removed from the home server, then local copy 
                   8816:       is unlinked.
                   8817:   (ii) If local copy does not exist -
                   8818:       requests the file from the home server and stores it. 
                   8819:   
                   8820:   If $caller is 'uploadrep':  
                   8821:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   8822:     for request for files originally uploaded via DOCS. 
                   8823:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   8824:   
                   8825:   Otherwise:
                   8826:      This indicates a call from the content generation phase of the request.
                   8827:      -  returns the entire contents of the file or -1.
                   8828:      
                   8829: (b) files in /res
                   8830:    - returns the entire contents of a file or -1; 
                   8831:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 8832: 
1.712     albertel 8833: 
                   8834: =item *
                   8835: 
                   8836: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   8837:                   reference
                   8838: 
                   8839: returns either a stat() list of data about the file or an empty list
                   8840: if the file doesn't exist or couldn't find out about it (connection
                   8841: problems or user unknown)
                   8842: 
1.191     harris41 8843: =item *
                   8844: 
1.243     albertel 8845: filelocation($dir,$file) : returns file system location of a file
                   8846: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   8847: directory that relative $file lookups are to looked in ($dir of /a/dir
                   8848: and a file of ../bob will become /a/bob)
1.191     harris41 8849: 
                   8850: =item *
                   8851: 
                   8852: hreflocation($dir,$file) : returns file system location or a URL; same as
                   8853: filelocation except for hrefs
                   8854: 
                   8855: =item *
                   8856: 
                   8857: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
                   8858: 
1.243     albertel 8859: =back
                   8860: 
1.608     albertel 8861: =head2 Usererfile file routines (/uploaded*)
                   8862: 
                   8863: =over 4
                   8864: 
                   8865: =item *
                   8866: 
                   8867: userfileupload(): main rotine for putting a file in a user or course's
                   8868:                   filespace, arguments are,
                   8869: 
1.620     albertel 8870:  formname - required - this is the name of the element in $env where the
1.608     albertel 8871:            filename, and the contents of the file to create/modifed exist
1.620     albertel 8872:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   8873:            contents of the file is located in $env{'form.'.$formname}
1.608     albertel 8874:  coursedoc - if true, store the file in the course of the active role
                   8875:              of the current user
                   8876:  subdir - required - subdirectory to put the file in under ../userfiles/
                   8877:          if undefined, it will be placed in "unknown"
                   8878: 
                   8879:  (This routine calls clean_filename() to remove any dangerous
                   8880:  characters from the filename, and then calls finuserfileupload() to
                   8881:  complete the transaction)
                   8882: 
                   8883:  returns either the url of the uploaded file (/uploaded/....) if successful
                   8884:  and /adm/notfound.html if unsuccessful
                   8885: 
                   8886: =item *
                   8887: 
                   8888: clean_filename(): routine for cleaing a filename up for storage in
                   8889:                  userfile space, argument is:
                   8890: 
                   8891:  filename - proposed filename
                   8892: 
                   8893: returns: the new clean filename
                   8894: 
                   8895: =item *
                   8896: 
                   8897: finishuserfileupload(): routine that creaes and sends the file to
                   8898: userspace, probably shouldn't be called directly
                   8899: 
                   8900:   docuname: username or courseid of destination for the file
                   8901:   docudom: domain of user/course of destination for the file
                   8902:   formname: same as for userfileupload()
                   8903:   fname: filename (inculding subdirectories) for the file
                   8904: 
                   8905:  returns either the url of the uploaded file (/uploaded/....) if successful
                   8906:  and /adm/notfound.html if unsuccessful
                   8907: 
                   8908: =item *
                   8909: 
                   8910: renameuserfile(): renames an existing userfile to a new name
                   8911: 
                   8912:   Args:
                   8913:    docuname: username or courseid of destination for the file
                   8914:    docudom: domain of user/course of destination for the file
                   8915:    old: current file name (including any subdirs under userfiles)
                   8916:    new: desired file name (including any subdirs under userfiles)
                   8917: 
                   8918: =item *
                   8919: 
                   8920: mkdiruserfile(): creates a directory is a userfiles dir
                   8921: 
                   8922:   Args:
                   8923:    docuname: username or courseid of destination for the file
                   8924:    docudom: domain of user/course of destination for the file
                   8925:    dir: dir to create (including any subdirs under userfiles)
                   8926: 
                   8927: =item *
                   8928: 
                   8929: removeuserfile(): removes a file that exists in userfiles
                   8930: 
                   8931:   Args:
                   8932:    docuname: username or courseid of destination for the file
                   8933:    docudom: domain of user/course of destination for the file
                   8934:    fname: filname to delete (including any subdirs under userfiles)
                   8935: 
                   8936: =item *
                   8937: 
                   8938: removeuploadedurl(): convience function for removeuserfile()
                   8939: 
                   8940:   Args:
                   8941:    url:  a full /uploaded/... url to delete
                   8942: 
1.747     albertel 8943: =item * 
                   8944: 
                   8945: get_portfile_permissions():
                   8946:   Args:
                   8947:     domain: domain of user or course contain the portfolio files
                   8948:     user: name of user or num of course contain the portfolio files
                   8949:   Returns:
                   8950:     hashref of a dump of the proper file_permissions.db
                   8951:    
                   8952: 
                   8953: =item * 
                   8954: 
                   8955: get_access_controls():
                   8956: 
                   8957: Args:
                   8958:   current_permissions: the hash ref returned from get_portfile_permissions()
                   8959:   group: (optional) the group you want the files associated with
                   8960:   file: (optional) the file you want access info on
                   8961: 
                   8962: Returns:
1.749     raeburn  8963:     a hash (keys are file names) of hashes containing
                   8964:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   8965:         values are XML containing access control settings (see below) 
1.747     albertel 8966: 
                   8967: Internal notes:
                   8968: 
1.749     raeburn  8969:  access controls are stored in file_permissions.db as key=value pairs.
                   8970:     key -> path to file/file_name\0uniqueID:scope_end_start
                   8971:         where scope -> public,guest,course,group,domains or users.
                   8972:               end -> UNIX time for end of access (0 -> no end date)
                   8973:               start -> UNIX time for start of access
                   8974: 
                   8975:     value -> XML description of access control
                   8976:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   8977:             <start></start>
                   8978:             <end></end>
                   8979: 
                   8980:             <password></password>  for scope type = guest
                   8981: 
                   8982:             <domain></domain>     for scope type = course or group
                   8983:             <number></number>
                   8984:             <roles id="">
                   8985:              <role></role>
                   8986:              <access></access>
                   8987:              <section></section>
                   8988:              <group></group>
                   8989:             </roles>
                   8990: 
                   8991:             <dom></dom>         for scope type = domains
                   8992: 
                   8993:             <users>             for scope type = users
                   8994:              <user>
                   8995:               <uname></uname>
                   8996:               <udom></udom>
                   8997:              </user>
                   8998:             </users>
                   8999:            </scope> 
                   9000:               
                   9001:  Access data is also aggregated for each file in an additional key=value pair:
                   9002:  key -> path to file/file_name\0accesscontrol 
                   9003:  value -> reference to hash
                   9004:           hash contains key = value pairs
                   9005:           where key = uniqueID:scope_end_start
                   9006:                 value = UNIX time record was last updated
                   9007: 
                   9008:           Used to improve speed of look-ups of access controls for each file.  
                   9009:  
                   9010:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   9011: 
                   9012: modify_access_controls():
                   9013: 
                   9014: Modifies access controls for a portfolio file
                   9015: Args
                   9016: 1. file name
                   9017: 2. reference to hash of required changes,
                   9018: 3. domain
                   9019: 4. username
                   9020:   where domain,username are the domain of the portfolio owner 
                   9021:   (either a user or a course) 
                   9022: 
                   9023: Returns:
                   9024: 1. result of additions or updates ('ok' or 'error', with error message). 
                   9025: 2. result of deletions ('ok' or 'error', with error message).
                   9026: 3. reference to hash of any new or updated access controls.
                   9027: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   9028:    key = integer (inbound ID)
                   9029:    value = uniqueID  
1.747     albertel 9030: 
1.608     albertel 9031: =back
                   9032: 
1.243     albertel 9033: =head2 HTTP Helper Routines
                   9034: 
                   9035: =over 4
                   9036: 
1.191     harris41 9037: =item *
                   9038: 
                   9039: escape() : unpack non-word characters into CGI-compatible hex codes
                   9040: 
                   9041: =item *
                   9042: 
                   9043: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   9044: 
1.243     albertel 9045: =back
                   9046: 
                   9047: =head1 PRIVATE SUBROUTINES
                   9048: 
                   9049: =head2 Underlying communication routines (Shouldn't call)
                   9050: 
                   9051: =over 4
                   9052: 
                   9053: =item *
                   9054: 
                   9055: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   9056: 
                   9057: =item *
                   9058: 
                   9059: reply() : uses subreply to send a message to remote machine, logs all failures
                   9060: 
                   9061: =item *
                   9062: 
                   9063: critical() : passes a critical message to another server; if cannot
                   9064: get through then place message in connection buffer directory and
                   9065: returns con_delayed, if incapable of saving message, returns
                   9066: con_failed
                   9067: 
                   9068: =item *
                   9069: 
                   9070: reconlonc() : tries to reconnect lonc client processes.
                   9071: 
                   9072: =back
                   9073: 
                   9074: =head2 Resource Access Logging
                   9075: 
                   9076: =over 4
                   9077: 
                   9078: =item *
                   9079: 
                   9080: flushcourselogs() : flush (save) buffer logs and access logs
                   9081: 
                   9082: =item *
                   9083: 
                   9084: courselog($what) : save message for course in hash
                   9085: 
                   9086: =item *
                   9087: 
                   9088: courseacclog($what) : save message for course using &courselog().  Perform
                   9089: special processing for specific resource types (problems, exams, quizzes, etc).
                   9090: 
1.191     harris41 9091: =item *
                   9092: 
                   9093: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   9094: as a PerlChildExitHandler
1.243     albertel 9095: 
                   9096: =back
                   9097: 
                   9098: =head2 Other
                   9099: 
                   9100: =over 4
                   9101: 
                   9102: =item *
                   9103: 
                   9104: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 9105: 
                   9106: =back
                   9107: 
                   9108: =cut

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