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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.543   ! albertel    4: # $Id: lonnet.pm,v 1.542 2004/09/17 02:40:35 albertel Exp $
1.178     www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.169     harris41   28: ###
                     29: 
1.1       albertel   30: package Apache::lonnet;
                     31: 
                     32: use strict;
1.8       www        33: use LWP::UserAgent();
1.15      www        34: use HTTP::Headers;
1.486     www        35: use HTTP::Date;
                     36: # use Date::Parse;
1.11      www        37: use vars 
1.300     albertel   38: qw(%perlvar %hostname %homecache %badServerCache %hostip %iphost %spareid %hostdom 
1.440     www        39:    %libserv %pr %prp %metacache %packagetab %titlecache %courseresversioncache %resversioncache
1.349     www        40:    %courselogs %accesshash %userrolehash $processmarker $dumpcount 
1.516     raeburn    41:    %coursedombuf %coursenumbuf %coursehombuf %coursedescrbuf %courseinstcodebuf %courseresdatacache 
1.420     albertel   42:    %userresdatacache %usectioncache %domaindescription %domain_auth_def %domain_auth_arg_def 
1.403     www        43:    %domain_lang_def %domain_city %domain_longi %domain_lati $tmpdir);
                     44: 
1.1       albertel   45: use IO::Socket;
1.31      www        46: use GDBM_File;
1.8       www        47: use Apache::Constants qw(:common :http);
1.208     albertel   48: use HTML::LCParser;
1.88      www        49: use Fcntl qw(:flock);
1.294     matthew    50: use Apache::loncoursedata;
1.414     www        51: use Apache::lonlocal;
1.428     albertel   52: use Storable qw(lock_store lock_nstore lock_retrieve freeze thaw);
1.539     albertel   53: use Time::HiRes qw( gettimeofday tv_interval );
1.195     www        54: my $readit;
1.1       albertel   55: 
1.449     matthew    56: =pod
                     57: 
                     58: =head1 Package Variables
                     59: 
                     60: These are largely undocumented, so if you decipher one please note it here.
                     61: 
                     62: =over 4
                     63: 
                     64: =item $processmarker
                     65: 
                     66: Contains the time this process was started and this servers host id.
                     67: 
                     68: =item $dumpcount
                     69: 
                     70: Counts the number of times a message log flush has been attempted (regardless
                     71: of success) by this process.  Used as part of the filename when messages are
                     72: delayed.
                     73: 
                     74: =back
                     75: 
                     76: =cut
                     77: 
                     78: 
1.1       albertel   79: # --------------------------------------------------------------------- Logging
                     80: 
1.163     harris41   81: sub logtouch {
                     82:     my $execdir=$perlvar{'lonDaemons'};
1.448     albertel   83:     unless (-e "$execdir/logs/lonnet.log") {	
                     84: 	open(my $fh,">>$execdir/logs/lonnet.log");
1.163     harris41   85: 	close $fh;
                     86:     }
                     87:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                     88:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                     89: }
                     90: 
1.1       albertel   91: sub logthis {
                     92:     my $message=shift;
                     93:     my $execdir=$perlvar{'lonDaemons'};
                     94:     my $now=time;
                     95:     my $local=localtime($now);
1.448     albertel   96:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
                     97: 	print $fh "$local ($$): $message\n";
                     98: 	close($fh);
                     99:     }
1.1       albertel  100:     return 1;
                    101: }
                    102: 
                    103: sub logperm {
                    104:     my $message=shift;
                    105:     my $execdir=$perlvar{'lonDaemons'};
                    106:     my $now=time;
                    107:     my $local=localtime($now);
1.448     albertel  108:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
                    109: 	print $fh "$now:$message:$local\n";
                    110: 	close($fh);
                    111:     }
1.1       albertel  112:     return 1;
                    113: }
                    114: 
                    115: # -------------------------------------------------- Non-critical communication
                    116: sub subreply {
                    117:     my ($cmd,$server)=@_;
                    118:     my $peerfile="$perlvar{'lonSockDir'}/$server";
                    119:     my $client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    120:                                      Type    => SOCK_STREAM,
                    121:                                      Timeout => 10)
                    122:        or return "con_lost";
                    123:     print $client "$cmd\n";
                    124:     my $answer=<$client>;
1.9       www       125:     if (!$answer) { $answer="con_lost"; }
1.1       albertel  126:     chomp($answer);
                    127:     return $answer;
                    128: }
                    129: 
                    130: sub reply {
                    131:     my ($cmd,$server)=@_;
1.205     www       132:     unless (defined($hostname{$server})) { return 'no_such_host'; }
1.1       albertel  133:     my $answer=subreply($cmd,$server);
1.203     www       134:     if ($answer eq 'con_lost') {
1.311     matthew   135:         #sleep 5; 
                    136:         #$answer=subreply($cmd,$server);
                    137:         #if ($answer eq 'con_lost') {
1.233     albertel  138: 	#   &logthis("Second attempt con_lost on $server");
                    139:         #   my $peerfile="$perlvar{'lonSockDir'}/$server";
                    140:         #   my $client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    141:         #                                    Type    => SOCK_STREAM,
                    142:         #                                    Timeout => 10)
                    143:         #              or return "con_lost";
                    144:         #   &logthis("Killing socket");
                    145:         #   print $client "close_connection_exit\n";
                    146:            #sleep 5;
                    147:         #   $answer=subreply($cmd,$server);       
                    148:        #}   
1.203     www       149:     }
1.65      www       150:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.12      www       151:        &logthis("<font color=blue>WARNING:".
                    152:                 " $cmd to $server returned $answer</font>");
                    153:     }
1.1       albertel  154:     return $answer;
                    155: }
                    156: 
                    157: # ----------------------------------------------------------- Send USR1 to lonc
                    158: 
                    159: sub reconlonc {
                    160:     my $peerfile=shift;
                    161:     &logthis("Trying to reconnect for $peerfile");
                    162:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448     albertel  163:     if (open(my $fh,"<$loncfile")) {
1.1       albertel  164: 	my $loncpid=<$fh>;
                    165:         chomp($loncpid);
                    166:         if (kill 0 => $loncpid) {
                    167: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    168:             kill USR1 => $loncpid;
                    169:             sleep 1;
                    170:             if (-e "$peerfile") { return; }
                    171:             &logthis("$peerfile still not there, give it another try");
                    172:             sleep 5;
                    173:             if (-e "$peerfile") { return; }
1.12      www       174:             &logthis(
                    175:   "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
1.1       albertel  176:         } else {
1.12      www       177: 	    &logthis(
                    178:                "<font color=blue>WARNING:".
                    179:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  180:         }
                    181:     } else {
1.12      www       182:      &logthis('<font color=blue>WARNING: lonc not running, giving up</font>');
1.1       albertel  183:     }
                    184: }
                    185: 
                    186: # ------------------------------------------------------ Critical communication
1.12      www       187: 
1.1       albertel  188: sub critical {
                    189:     my ($cmd,$server)=@_;
1.89      www       190:     unless ($hostname{$server}) {
                    191:         &logthis("<font color=blue>WARNING:".
                    192:                " Critical message to unknown server ($server)</font>");
                    193:         return 'no_such_host';
                    194:     }
1.1       albertel  195:     my $answer=reply($cmd,$server);
                    196:     if ($answer eq 'con_lost') {
                    197:         my $pingreply=reply('ping',$server);
                    198: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
                    199:         my $pongreply=reply('pong',$server);
                    200:         &logthis("Ping/Pong for $server: $pingreply/$pongreply");
                    201:         $answer=reply($cmd,$server);
                    202:         if ($answer eq 'con_lost') {
                    203:             my $now=time;
                    204:             my $middlename=$cmd;
1.5       www       205:             $middlename=substr($middlename,0,16);
1.1       albertel  206:             $middlename=~s/\W//g;
                    207:             my $dfilename=
1.305     www       208:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    209:             $dumpcount++;
1.1       albertel  210:             {
1.448     albertel  211: 		my $dfh;
                    212: 		if (open($dfh,">$dfilename")) {
                    213: 		    print $dfh "$cmd\n"; 
                    214: 		    close($dfh);
                    215: 		}
1.1       albertel  216:             }
                    217:             sleep 2;
                    218:             my $wcmd='';
                    219:             {
1.448     albertel  220: 		my $dfh;
                    221: 		if (open($dfh,"<$dfilename")) {
                    222: 		    $wcmd=<$dfh>; 
                    223: 		    close($dfh);
                    224: 		}
1.1       albertel  225:             }
                    226:             chomp($wcmd);
1.7       www       227:             if ($wcmd eq $cmd) {
1.12      www       228: 		&logthis("<font color=blue>WARNING: ".
                    229:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  230:                 &logperm("D:$server:$cmd");
                    231: 	        return 'con_delayed';
                    232:             } else {
1.12      www       233:                 &logthis("<font color=red>CRITICAL:"
                    234:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  235:                 &logperm("F:$server:$cmd");
                    236:                 return 'con_failed';
                    237:             }
                    238:         }
                    239:     }
                    240:     return $answer;
1.405     albertel  241: }
                    242: 
1.412     www       243: #
1.405     albertel  244: # -------------- Remove all key from the env that start witha lowercase letter
1.412     www       245: #                (Which is always a lon-capa value)
                    246: 
1.405     albertel  247: sub cleanenv {
1.412     www       248: #    unless (defined(&Apache::exists_config_define("MODPERL2"))) { return; }
                    249: #    unless (&Apache::exists_config_define("MODPERL2")) { return; }
1.405     albertel  250:     foreach my $key (keys(%ENV)) {
                    251: 	if ($key =~ /^[a-z]/) {
                    252: 	    delete($ENV{$key});
                    253: 	}
                    254:     }
1.374     www       255: }
                    256:  
                    257: # ------------------------------------------- Transfer profile into environment
                    258: 
                    259: sub transfer_profile_to_env {
                    260:     my ($lonidsdir,$handle)=@_;
                    261:     my @profile;
                    262:     {
1.448     albertel  263: 	open(my $idf,"$lonidsdir/$handle.id");
1.374     www       264: 	flock($idf,LOCK_SH);
                    265: 	@profile=<$idf>;
1.448     albertel  266: 	close($idf);
1.374     www       267:     }
                    268:     my $envi;
1.433     matthew   269:     my %Remove;
1.374     www       270:     for ($envi=0;$envi<=$#profile;$envi++) {
                    271: 	chomp($profile[$envi]);
                    272: 	my ($envname,$envvalue)=split(/=/,$profile[$envi]);
                    273: 	$ENV{$envname} = $envvalue;
1.433     matthew   274:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    275:             if ($time < time-300) {
                    276:                 $Remove{$key}++;
                    277:             }
                    278:         }
                    279:     }
1.446     albertel  280:     $ENV{'user.environment'} = "$lonidsdir/$handle.id";
1.433     matthew   281:     foreach my $expired_key (keys(%Remove)) {
                    282:         &delenv($expired_key);
1.374     www       283:     }
1.1       albertel  284: }
                    285: 
1.5       www       286: # ---------------------------------------------------------- Append Environment
                    287: 
                    288: sub appenv {
1.6       www       289:     my %newenv=@_;
1.191     harris41  290:     foreach (keys %newenv) {
1.35      www       291: 	if (($newenv{$_}=~/^user\.role/) || ($newenv{$_}=~/^user\.priv/)) {
                    292:             &logthis("<font color=blue>WARNING: ".
1.151     www       293:                 "Attempt to modify environment ".$_." to ".$newenv{$_}
                    294:                 .'</font>');
1.35      www       295: 	    delete($newenv{$_});
                    296:         } else {
                    297:             $ENV{$_}=$newenv{$_};
                    298:         }
1.191     harris41  299:     }
1.95      www       300: 
                    301:     my $lockfh;
1.448     albertel  302:     unless (open($lockfh,"$ENV{'user.environment'}")) {
                    303: 	return 'error: '.$!;
1.95      www       304:     }
                    305:     unless (flock($lockfh,LOCK_EX)) {
                    306:          &logthis("<font color=blue>WARNING: ".
                    307:                   'Could not obtain exclusive lock in appenv: '.$!);
1.448     albertel  308:          close($lockfh);
1.95      www       309:          return 'error: '.$!;
                    310:     }
                    311: 
1.6       www       312:     my @oldenv;
                    313:     {
1.448     albertel  314: 	my $fh;
                    315: 	unless (open($fh,"$ENV{'user.environment'}")) {
                    316: 	    return 'error: '.$!;
                    317: 	}
                    318: 	@oldenv=<$fh>;
                    319: 	close($fh);
1.6       www       320:     }
                    321:     for (my $i=0; $i<=$#oldenv; $i++) {
                    322:         chomp($oldenv[$i]);
1.9       www       323:         if ($oldenv[$i] ne '') {
1.448     albertel  324: 	    my ($name,$value)=split(/=/,$oldenv[$i]);
                    325: 	    unless (defined($newenv{$name})) {
                    326: 		$newenv{$name}=$value;
                    327: 	    }
1.9       www       328:         }
1.6       www       329:     }
                    330:     {
1.448     albertel  331: 	my $fh;
                    332: 	unless (open($fh,">$ENV{'user.environment'}")) {
                    333: 	    return 'error';
                    334: 	}
                    335: 	my $newname;
                    336: 	foreach $newname (keys %newenv) {
                    337: 	    print $fh "$newname=$newenv{$newname}\n";
                    338: 	}
                    339: 	close($fh);
1.56      www       340:     }
1.448     albertel  341: 	
                    342:     close($lockfh);
1.56      www       343:     return 'ok';
                    344: }
                    345: # ----------------------------------------------------- Delete from Environment
                    346: 
                    347: sub delenv {
                    348:     my $delthis=shift;
                    349:     my %newenv=();
                    350:     if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
                    351:         &logthis("<font color=blue>WARNING: ".
                    352:                 "Attempt to delete from environment ".$delthis);
                    353:         return 'error';
                    354:     }
                    355:     my @oldenv;
                    356:     {
1.448     albertel  357: 	my $fh;
                    358: 	unless (open($fh,"$ENV{'user.environment'}")) {
                    359: 	    return 'error';
                    360: 	}
                    361: 	unless (flock($fh,LOCK_SH)) {
                    362: 	    &logthis("<font color=blue>WARNING: ".
                    363: 		     'Could not obtain shared lock in delenv: '.$!);
                    364: 	    close($fh);
                    365: 	    return 'error: '.$!;
                    366: 	}
                    367: 	@oldenv=<$fh>;
                    368: 	close($fh);
1.56      www       369:     }
                    370:     {
1.448     albertel  371: 	my $fh;
                    372: 	unless (open($fh,">$ENV{'user.environment'}")) {
                    373: 	    return 'error';
                    374: 	}
                    375: 	unless (flock($fh,LOCK_EX)) {
                    376: 	    &logthis("<font color=blue>WARNING: ".
                    377: 		     'Could not obtain exclusive lock in delenv: '.$!);
                    378: 	    close($fh);
                    379: 	    return 'error: '.$!;
                    380: 	}
                    381: 	foreach (@oldenv) {
1.473     matthew   382: 	    if ($_=~/^$delthis/) { 
                    383:                 my ($key,undef) = split('=',$_);
                    384:                 delete($ENV{$key});
                    385:             } else {
                    386:                 print $fh $_; 
                    387:             }
1.448     albertel  388: 	}
                    389: 	close($fh);
1.5       www       390:     }
                    391:     return 'ok';
1.369     albertel  392: }
                    393: 
                    394: # ------------------------------------------ Find out current server userload
                    395: # there is a copy in lond
                    396: sub userload {
                    397:     my $numusers=0;
                    398:     {
                    399: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                    400: 	my $filename;
                    401: 	my $curtime=time;
                    402: 	while ($filename=readdir(LONIDS)) {
                    403: 	    if ($filename eq '.' || $filename eq '..') {next;}
1.404     albertel  404: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel  405: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel  406: 	}
                    407: 	closedir(LONIDS);
                    408:     }
                    409:     my $userloadpercent=0;
                    410:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                    411:     if ($maxuserload) {
1.371     albertel  412: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel  413:     }
1.372     albertel  414:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel  415:     return $userloadpercent;
1.283     www       416: }
                    417: 
                    418: # ------------------------------------------ Fight off request when overloaded
                    419: 
                    420: sub overloaderror {
                    421:     my ($r,$checkserver)=@_;
                    422:     unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
                    423:     my $loadavg;
                    424:     if ($checkserver eq $perlvar{'lonHostID'}) {
1.448     albertel  425:        open(my $loadfile,'/proc/loadavg');
1.283     www       426:        $loadavg=<$loadfile>;
                    427:        $loadavg =~ s/\s.*//g;
1.285     matthew   428:        $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.448     albertel  429:        close($loadfile);
1.283     www       430:     } else {
                    431:        $loadavg=&reply('load',$checkserver);
                    432:     }
1.285     matthew   433:     my $overload=$loadavg-100;
1.283     www       434:     if ($overload>0) {
1.285     matthew   435: 	$r->err_headers_out->{'Retry-After'}=$overload;
1.283     www       436:         $r->log_error('Overload of '.$overload.' on '.$checkserver);
1.502     matthew   437:         return 409;
1.283     www       438:     }    
                    439:     return '';
1.5       www       440: }
1.1       albertel  441: 
                    442: # ------------------------------ Find server with least workload from spare.tab
1.11      www       443: 
1.1       albertel  444: sub spareserver {
1.370     albertel  445:     my ($loadpercent,$userloadpercent) = @_;
1.1       albertel  446:     my $tryserver;
                    447:     my $spareserver='';
1.370     albertel  448:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
                    449:     my $lowestserver=$loadpercent > $userloadpercent?
                    450: 	             $loadpercent :  $userloadpercent;
1.1       albertel  451:     foreach $tryserver (keys %spareid) {
1.411     albertel  452: 	my $loadans=reply('load',$tryserver);
                    453: 	my $userloadans=reply('userload',$tryserver);
                    454: 	if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
                    455: 	    next; #didn't get a number from the server
                    456: 	}
                    457: 	my $answer;
                    458: 	if ($loadans =~ /\d/) {
                    459: 	    if ($userloadans =~ /\d/) {
                    460: 		#both are numbers, pick the bigger one
                    461: 		$answer=$loadans > $userloadans?
                    462: 		    $loadans :  $userloadans;
                    463: 	    } else {
                    464: 		$answer = $loadans;
                    465: 	    }
                    466: 	} else {
                    467: 	    $answer = $userloadans;
                    468: 	}
                    469: 	if (($answer =~ /\d/) && ($answer<$lowestserver)) {
                    470: 	    $spareserver="http://$hostname{$tryserver}";
                    471: 	    $lowestserver=$answer;
                    472: 	}
1.370     albertel  473:     }
1.1       albertel  474:     return $spareserver;
1.202     matthew   475: }
                    476: 
                    477: # --------------------------------------------- Try to change a user's password
                    478: 
                    479: sub changepass {
                    480:     my ($uname,$udom,$currentpass,$newpass,$server)=@_;
                    481:     $currentpass = &escape($currentpass);
                    482:     $newpass     = &escape($newpass);
                    483:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass",
                    484: 		       $server);
                    485:     if (! $answer) {
                    486: 	&logthis("No reply on password change request to $server ".
                    487: 		 "by $uname in domain $udom.");
                    488:     } elsif ($answer =~ "^ok") {
                    489:         &logthis("$uname in $udom successfully changed their password ".
                    490: 		 "on $server.");
                    491:     } elsif ($answer =~ "^pwchange_failure") {
                    492: 	&logthis("$uname in $udom was unable to change their password ".
                    493: 		 "on $server.  The action was blocked by either lcpasswd ".
                    494: 		 "or pwchange");
                    495:     } elsif ($answer =~ "^non_authorized") {
                    496:         &logthis("$uname in $udom did not get their password correct when ".
                    497: 		 "attempting to change it on $server.");
                    498:     } elsif ($answer =~ "^auth_mode_error") {
                    499:         &logthis("$uname in $udom attempted to change their password despite ".
                    500: 		 "not being locally or internally authenticated on $server.");
                    501:     } elsif ($answer =~ "^unknown_user") {
                    502:         &logthis("$uname in $udom attempted to change their password ".
                    503: 		 "on $server but were unable to because $server is not ".
                    504: 		 "their home server.");
                    505:     } elsif ($answer =~ "^refused") {
                    506: 	&logthis("$server refused to change $uname in $udom password because ".
                    507: 		 "it was sent an unencrypted request to change the password.");
                    508:     }
                    509:     return $answer;
1.1       albertel  510: }
                    511: 
1.169     harris41  512: # ----------------------- Try to determine user's current authentication scheme
                    513: 
                    514: sub queryauthenticate {
                    515:     my ($uname,$udom)=@_;
1.456     albertel  516:     my $uhome=&homeserver($uname,$udom);
                    517:     if (!$uhome) {
                    518: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                    519: 	return 'no_host';
                    520:     }
                    521:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                    522:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                    523: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41  524:     }
1.456     albertel  525:     return $answer;
1.169     harris41  526: }
                    527: 
1.1       albertel  528: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www       529: 
1.1       albertel  530: sub authenticate {
                    531:     my ($uname,$upass,$udom)=@_;
1.12      www       532:     $upass=escape($upass);
1.199     www       533:     $uname=~s/\W//g;
1.471     albertel  534:     my $uhome=&homeserver($uname,$udom);
                    535:     if (!$uhome) {
                    536: 	&logthis("User $uname at $udom is unknown in authenticate");
                    537: 	return 'no_host';
1.1       albertel  538:     }
1.471     albertel  539:     my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
                    540:     if ($answer eq 'authorized') {
                    541: 	&logthis("User $uname at $udom authorized by $uhome"); 
                    542: 	return $uhome; 
                    543:     }
                    544:     if ($answer eq 'non_authorized') {
                    545: 	&logthis("User $uname at $udom rejected by $uhome");
                    546: 	return 'no_host'; 
1.9       www       547:     }
1.471     albertel  548:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel  549:     return 'no_host';
                    550: }
                    551: 
                    552: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www       553: 
1.1       albertel  554: sub homeserver {
1.230     stredwic  555:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel  556:     my $index="$uname:$udom";
1.426     albertel  557: 
                    558:     my ($result,$cached)=&is_cached(\%homecache,$index,'home',86400);
                    559:     if (defined($cached)) { return $result; }
1.1       albertel  560:     my $tryserver;
                    561:     foreach $tryserver (keys %libserv) {
1.230     stredwic  562:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic  563: 		 exists($badServerCache{$tryserver}));
1.1       albertel  564: 	if ($hostdom{$tryserver} eq $udom) {
                    565:            my $answer=reply("home:$udom:$uname",$tryserver);
                    566:            if ($answer eq 'found') { 
1.426     albertel  567: 	       return &do_cache(\%homecache,$index,$tryserver,'home');
1.231     stredwic  568:            } elsif ($answer eq 'no_host') {
                    569: 	       $badServerCache{$tryserver}=1;
1.221     matthew   570:            }
1.1       albertel  571:        }
                    572:     }    
                    573:     return 'no_host';
1.70      www       574: }
                    575: 
                    576: # ------------------------------------- Find the usernames behind a list of IDs
                    577: 
                    578: sub idget {
                    579:     my ($udom,@ids)=@_;
                    580:     my %returnhash=();
                    581:     
                    582:     my $tryserver;
                    583:     foreach $tryserver (keys %libserv) {
                    584:        if ($hostdom{$tryserver} eq $udom) {
                    585: 	  my $idlist=join('&',@ids);
                    586:           $idlist=~tr/A-Z/a-z/; 
                    587: 	  my $reply=&reply("idget:$udom:".$idlist,$tryserver);
                    588:           my @answer=();
1.76      www       589:           if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1.70      www       590: 	      @answer=split(/\&/,$reply);
                    591:           }                    ;
                    592:           my $i;
                    593:           for ($i=0;$i<=$#ids;$i++) {
                    594:               if ($answer[$i]) {
                    595: 		  $returnhash{$ids[$i]}=$answer[$i];
                    596:               } 
                    597:           }
                    598:        }
                    599:     }    
                    600:     return %returnhash;
                    601: }
                    602: 
                    603: # ------------------------------------- Find the IDs behind a list of usernames
                    604: 
                    605: sub idrget {
                    606:     my ($udom,@unames)=@_;
                    607:     my %returnhash=();
1.191     harris41  608:     foreach (@unames) {
1.70      www       609:         $returnhash{$_}=(&userenvironment($udom,$_,'id'))[1];
1.191     harris41  610:     }
1.70      www       611:     return %returnhash;
                    612: }
                    613: 
                    614: # ------------------------------- Store away a list of names and associated IDs
                    615: 
                    616: sub idput {
                    617:     my ($udom,%ids)=@_;
                    618:     my %servers=();
1.191     harris41  619:     foreach (keys %ids) {
1.487     albertel  620: 	&cput('environment',{'id'=>$ids{$_}},$udom,$_);
1.70      www       621:         my $uhom=&homeserver($_,$udom);
                    622:         if ($uhom ne 'no_host') {
                    623:             my $id=&escape($ids{$_});
                    624:             $id=~tr/A-Z/a-z/;
                    625:             my $unam=&escape($_);
                    626: 	    if ($servers{$uhom}) {
                    627: 		$servers{$uhom}.='&'.$id.'='.$unam;
                    628:             } else {
                    629:                 $servers{$uhom}=$id.'='.$unam;
                    630:             }
                    631:         }
1.191     harris41  632:     }
                    633:     foreach (keys %servers) {
1.70      www       634:         &critical('idput:'.$udom.':'.$servers{$_},$_);
1.191     harris41  635:     }
1.344     www       636: }
                    637: 
                    638: # --------------------------------------------------- Assign a key to a student
                    639: 
                    640: sub assign_access_key {
1.364     www       641: #
                    642: # a valid key looks like uname:udom#comments
                    643: # comments are being appended
                    644: #
1.498     www       645:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                    646:     $kdom=
                    647:    $ENV{'course.'.$ENV{'request.course.id'}.'.domain'} unless (defined($kdom));
                    648:     $knum=
                    649:    $ENV{'course.'.$ENV{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www       650:     $cdom=
                    651:    $ENV{'course.'.$ENV{'request.course.id'}.'.domain'} unless (defined($cdom));
                    652:     $cnum=
                    653:    $ENV{'course.'.$ENV{'request.course.id'}.'.num'} unless (defined($cnum));
                    654:     $udom=$ENV{'user.name'} unless (defined($udom));
                    655:     $uname=$ENV{'user.domain'} unless (defined($uname));
1.498     www       656:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www       657:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel  658:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www       659:                                                   # assigned to this person
                    660:                                                   # - this should not happen,
1.345     www       661:                                                   # unless something went wrong
                    662:                                                   # the first time around
                    663: # ready to assign
1.364     www       664:         $logentry=$1.'; '.$logentry;
1.496     www       665:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www       666:                                                  $kdom,$knum) eq 'ok') {
1.345     www       667: # key now belongs to user
1.346     www       668: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www       669:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
                    670:                 &appenv('environment.'.$envkey => $ckey);
                    671:                 return 'ok';
                    672:             } else {
                    673:                 return 
                    674:   'error: Count not permanently assign key, will need to be re-entered later.';
                    675: 	    }
                    676:         } else {
                    677:             return 'error: Could not assign key, try again later.';
                    678:         }
1.364     www       679:     } elsif (!$existing{$ckey}) {
1.345     www       680: # the key does not exist
                    681: 	return 'error: The key does not exist';
                    682:     } else {
                    683: # the key is somebody else's
                    684: 	return 'error: The key is already in use';
                    685:     }
1.344     www       686: }
                    687: 
1.364     www       688: # ------------------------------------------ put an additional comment on a key
                    689: 
                    690: sub comment_access_key {
                    691: #
                    692: # a valid key looks like uname:udom#comments
                    693: # comments are being appended
                    694: #
                    695:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                    696:     $cdom=
                    697:    $ENV{'course.'.$ENV{'request.course.id'}.'.domain'} unless (defined($cdom));
                    698:     $cnum=
                    699:    $ENV{'course.'.$ENV{'request.course.id'}.'.num'} unless (defined($cnum));
                    700:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                    701:     if ($existing{$ckey}) {
                    702:         $existing{$ckey}.='; '.$logentry;
                    703: # ready to assign
1.367     www       704:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www       705:                                                  $cdom,$cnum) eq 'ok') {
                    706: 	    return 'ok';
                    707:         } else {
                    708: 	    return 'error: Count not store comment.';
                    709:         }
                    710:     } else {
                    711: # the key does not exist
                    712: 	return 'error: The key does not exist';
                    713:     }
                    714: }
                    715: 
1.344     www       716: # ------------------------------------------------------ Generate a set of keys
                    717: 
                    718: sub generate_access_keys {
1.364     www       719:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www       720:     $cdom=
                    721:    $ENV{'course.'.$ENV{'request.course.id'}.'.domain'} unless (defined($cdom));
                    722:     $cnum=
                    723:    $ENV{'course.'.$ENV{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www       724:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www       725:     unless (($cdom) && ($cnum)) { return 0; }
                    726:     if ($number>10000) { return 0; }
                    727:     sleep(2); # make sure don't get same seed twice
                    728:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                    729:     my $total=0;
                    730:     for (my $i=1;$i<=$number;$i++) {
                    731:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                    732:                   sprintf("%lx",int(100000*rand)).'-'.
                    733:                   sprintf("%lx",int(100000*rand));
                    734:        $newkey=~s/1/g/g; # folks mix up 1 and l
                    735:        $newkey=~s/0/h/g; # and also 0 and O
                    736:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                    737:        if ($existing{$newkey}) {
                    738:            $i--;
                    739:        } else {
1.364     www       740: 	  if (&put('accesskeys',
                    741:               { $newkey => '# generated '.localtime().
                    742:                            ' by '.$ENV{'user.name'}.'@'.$ENV{'user.domain'}.
                    743:                            '; '.$logentry },
                    744: 		   $cdom,$cnum) eq 'ok') {
1.344     www       745:               $total++;
                    746: 	  }
                    747:        }
                    748:     }
                    749:     &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.home'},
                    750:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                    751:     return $total;
                    752: }
                    753: 
                    754: # ------------------------------------------------------- Validate an accesskey
                    755: 
                    756: sub validate_access_key {
                    757:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                    758:     $cdom=
                    759:    $ENV{'course.'.$ENV{'request.course.id'}.'.domain'} unless (defined($cdom));
                    760:     $cnum=
                    761:    $ENV{'course.'.$ENV{'request.course.id'}.'.num'} unless (defined($cnum));
1.497     www       762:     $udom=$ENV{'user.domain'} unless (defined($udom));
                    763:     $uname=$ENV{'user.name'} unless (defined($uname));
1.345     www       764:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel  765:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www       766: }
                    767: 
                    768: # ------------------------------------- Find the section of student in a course
1.298     matthew   769: 
                    770: sub getsection {
                    771:     my ($udom,$unam,$courseid)=@_;
                    772:     $courseid=~s/\_/\//g;
                    773:     $courseid=~s/^(\w)/\/$1/;
                    774:     my %Pending; 
                    775:     my %Expired;
                    776:     #
                    777:     # Each role can either have not started yet (pending), be active, 
                    778:     #    or have expired.
                    779:     #
                    780:     # If there is an active role, we are done.
                    781:     #
                    782:     # If there is more than one role which has not started yet, 
                    783:     #     choose the one which will start sooner
                    784:     # If there is one role which has not started yet, return it.
                    785:     #
                    786:     # If there is more than one expired role, choose the one which ended last.
                    787:     # If there is a role which has expired, return it.
                    788:     #
                    789:     foreach (split(/\&/,&reply('dump:'.$udom.':'.$unam.':roles',
                    790:                         &homeserver($unam,$udom)))) {
                    791:         my ($key,$value)=split(/\=/,$_);
                    792:         $key=&unescape($key);
1.479     albertel  793:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew   794:         my $section=$1;
                    795:         if ($key eq $courseid.'_st') { $section=''; }
                    796:         my ($dummy,$end,$start)=split(/\_/,&unescape($value));
                    797:         my $now=time;
                    798:         if (defined($end) && ($now > $end)) {
                    799:             $Expired{$end}=$section;
                    800:             next;
                    801:         }
                    802:         if (defined($start) && ($now < $start)) {
                    803:             $Pending{$start}=$section;
                    804:             next;
                    805:         }
                    806:         return $section;
                    807:     }
                    808:     #
                    809:     # Presumedly there will be few matching roles from the above
                    810:     # loop and the sorting time will be negligible.
                    811:     if (scalar(keys(%Pending))) {
                    812:         my ($time) = sort {$a <=> $b} keys(%Pending);
                    813:         return $Pending{$time};
                    814:     } 
                    815:     if (scalar(keys(%Expired))) {
                    816:         my @sorted = sort {$a <=> $b} keys(%Expired);
                    817:         my $time = pop(@sorted);
                    818:         return $Expired{$time};
                    819:     }
                    820:     return '-1';
                    821: }
1.70      www       822: 
1.452     albertel  823: 
1.543   ! albertel  824: my $disk_caching_disabled=1;
1.452     albertel  825: 
1.416     albertel  826: sub devalidate_cache {
1.428     albertel  827:     my ($cache,$id,$name) = @_;
1.417     albertel  828:     delete $$cache{$id.'.time'};
1.543   ! albertel  829:     delete $$cache{$id.'.file'};
1.417     albertel  830:     delete $$cache{$id};
1.541     albertel  831:     if (1 || $disk_caching_disabled) { return; }
1.442     albertel  832:     my $filename=$perlvar{'lonDaemons'}.'/tmp/lonnet_internal_cache_'.$name.".db";
1.541     albertel  833:     if (!-e $filename) { return; }
                    834:     open(DB,">$filename.lock");
1.428     albertel  835:     flock(DB,LOCK_EX);
                    836:     my %hash;
                    837:     if (tie(%hash,'GDBM_File',$filename,&GDBM_WRCREAT(),0640)) {
1.442     albertel  838: 	eval <<'EVALBLOCK';
                    839: 	    delete($hash{$id});
                    840: 	    delete($hash{$id.'.time'});
                    841: EVALBLOCK
                    842:         if ($@) {
                    843: 	    &logthis("<font color='red'>devalidate_cache blew up :$@:$name</font>");
                    844: 	    unlink($filename);
                    845: 	}
1.428     albertel  846:     } else {
1.442     albertel  847: 	if (-e $filename) {
                    848: 	    &logthis("Unable to tie hash (devalidate cache): $name");
                    849: 	    unlink($filename);
                    850: 	}
1.428     albertel  851:     }
                    852:     untie(%hash);
                    853:     flock(DB,LOCK_UN);
                    854:     close(DB);
1.416     albertel  855: }
                    856: 
                    857: sub is_cached {
1.425     albertel  858:     my ($cache,$id,$name,$time) = @_;
1.420     albertel  859:     if (!$time) { $time=300; }
1.416     albertel  860:     if (!exists($$cache{$id.'.time'})) {
1.542     albertel  861: 	&load_cache_item($cache,$name,$id,$time);
1.425     albertel  862:     }
                    863:     if (!exists($$cache{$id.'.time'})) {
                    864: #	&logthis("Didn't find $id");
1.417     albertel  865: 	return (undef,undef);
1.416     albertel  866:     } else {
1.425     albertel  867: 	if (time-($$cache{$id.'.time'})>$time) {
1.543   ! albertel  868: 	    if (exists($$cache{$id.'.file'})) {
        !           869: 		foreach my $filename (@{ $$cache{$id.'.file'} }) {
        !           870: 		    my $mtime=(stat($filename))[9];
        !           871: 		    #+1 is to take care of edge effects
        !           872: 		    if ($mtime && (($mtime+1) < ($$cache{$id.'.time'}))) {
        !           873: #			&logthis("Upping $mtime - ".$$cache{$id.'.time'}.
        !           874: #				 "$id because of $filename");
        !           875: 		    } else {
        !           876: 			&logthis("Devalidating $filename $id - ".(time-($$cache{$id.'.time'})));
        !           877: 			&devalidate_cache($cache,$id,$name);
        !           878: 			return (undef,undef);
        !           879: 		    }
        !           880: 		}
        !           881: 		$$cache{$id.'.time'}=time;
        !           882: 	    } else {
        !           883: #		&logthis("Devalidating $id - ".time-($$cache{$id.'.time'}));
        !           884: 		&devalidate_cache($cache,$id,$name);
        !           885: 		return (undef,undef);
        !           886: 	    }
1.416     albertel  887: 	}
                    888:     }
1.417     albertel  889:     return ($$cache{$id},1);
1.416     albertel  890: }
                    891: 
                    892: sub do_cache {
1.425     albertel  893:     my ($cache,$id,$value,$name) = @_;
1.416     albertel  894:     $$cache{$id.'.time'}=time;
1.425     albertel  895:     $$cache{$id}=$value;
1.428     albertel  896: #    &logthis("Caching $id as :$value:");
                    897:     &save_cache_item($cache,$name,$id);
1.416     albertel  898:     # do_cache implictly return the set value
1.425     albertel  899:     $$cache{$id};
                    900: }
                    901: 
1.541     albertel  902: my %do_save_item;
                    903: my %do_save;
1.428     albertel  904: sub save_cache_item {
                    905:     my ($cache,$name,$id)=@_;
1.452     albertel  906:     if ($disk_caching_disabled) { return; }
1.541     albertel  907:     $do_save{$name}=$cache;
                    908:     if (!exists($do_save_item{$name})) { $do_save_item{$name}={} }
                    909:     $do_save_item{$name}->{$id}=1;
                    910:     return;
                    911: }
                    912: 
                    913: sub save_cache {
                    914:     if ($disk_caching_disabled) { return; }
                    915:     my ($cache,$name,$id);
                    916:     foreach $name (keys(%do_save)) {
                    917: 	$cache=$do_save{$name};
                    918: 
                    919: 	my $starttime=&Time::HiRes::time();
                    920: 	&logthis("Saving :$name:");
                    921: 	my %hash;
                    922: 	my $filename=$perlvar{'lonDaemons'}.'/tmp/lonnet_internal_cache_'.$name.".db";
                    923: 	open(DB,">$filename.lock");
                    924: 	flock(DB,LOCK_EX);
                    925: 	if (tie(%hash,'GDBM_File',$filename,&GDBM_WRCREAT(),0640)) {
                    926: 	    foreach $id (keys(%{ $do_save_item{$name} })) {
                    927: 		eval <<'EVALBLOCK';
                    928: 		$hash{$id.'.time'}=$$cache{$id.'.time'};
                    929: 		$hash{$id}=freeze({'item'=>$$cache{$id}});
1.442     albertel  930: EVALBLOCK
1.541     albertel  931:                 if ($@) {
                    932: 		    &logthis("<font color='red'>save_cache blew up :$@:$name</font>");
                    933: 		    unlink($filename);
                    934: 		    last;
                    935: 		}
                    936: 	    }
                    937: 	} else {
                    938: 	    if (-e $filename) {
                    939: 		&logthis("Unable to tie hash (save cache): $name ($!)");
                    940: 		unlink($filename);
                    941: 	    }
1.442     albertel  942: 	}
1.541     albertel  943: 	untie(%hash);
                    944: 	flock(DB,LOCK_UN);
                    945: 	close(DB);
                    946: 	&logthis("save_cache $name took ".(&Time::HiRes::time()-$starttime));
1.428     albertel  947:     }
1.541     albertel  948:     undef(%do_save);
                    949:     undef(%do_save_item);
                    950: 
1.428     albertel  951: }
                    952: 
                    953: sub load_cache_item {
1.542     albertel  954:     my ($cache,$name,$id,$time)=@_;
1.452     albertel  955:     if ($disk_caching_disabled) { return; }
1.428     albertel  956:     my $starttime=&Time::HiRes::time();
                    957: #    &logthis("Before Loading $name  for $id size is ".scalar(%$cache));
                    958:     my %hash;
1.442     albertel  959:     my $filename=$perlvar{'lonDaemons'}.'/tmp/lonnet_internal_cache_'.$name.".db";
1.541     albertel  960:     if (!-e $filename) { return; }
                    961:     open(DB,">$filename.lock");
1.428     albertel  962:     flock(DB,LOCK_SH);
                    963:     if (tie(%hash,'GDBM_File',$filename,&GDBM_READER(),0640)) {
1.442     albertel  964: 	eval <<'EVALBLOCK';
                    965: 	    if (!%$cache) {
                    966: 		my $count;
                    967: 		while (my ($key,$value)=each(%hash)) { 
                    968: 		    $count++;
                    969: 		    if ($key =~ /\.time$/) {
                    970: 			$$cache{$key}=$value;
                    971: 		    } else {
                    972: 			my $hashref=thaw($value);
                    973: 			$$cache{$key}=$hashref->{'item'};
                    974: 		    }
1.428     albertel  975: 		}
1.442     albertel  976: #	    &logthis("Initial load: $count");
                    977: 	    } else {
1.542     albertel  978: 		if (($$cache{$id.'.time'}+$time) < time) {
                    979: 		    $$cache{$id.'.time'}=$hash{$id.'.time'};
                    980: 		    my $hashref=thaw($hash{$id});
                    981: 		    $$cache{$id}=$hashref->{'item'};
                    982: 		}
1.428     albertel  983: 	    }
1.442     albertel  984: EVALBLOCK
                    985:         if ($@) {
                    986: 	    &logthis("<font color='red'>load_cache blew up :$@:$name</font>");
                    987: 	    unlink($filename);
                    988: 	}        
                    989:     } else {
                    990: 	if (-e $filename) {
1.445     www       991: 	    &logthis("Unable to tie hash (load cache item): $name ($!)");
1.442     albertel  992: 	    unlink($filename);
1.428     albertel  993: 	}
                    994:     }
                    995:     untie(%hash);
                    996:     flock(DB,LOCK_UN);
                    997:     close(DB);
                    998: #    &logthis("After Loading $name size is ".scalar(%$cache));
                    999: #    &logthis("load_cache_item $name took ".(&Time::HiRes::time()-$starttime));
                   1000: }
                   1001: 
1.70      www      1002: sub usection {
                   1003:     my ($udom,$unam,$courseid)=@_;
1.416     albertel 1004:     my $hashid="$udom:$unam:$courseid";
                   1005:     
1.425     albertel 1006:     my ($result,$cached)=&is_cached(\%usectioncache,$hashid,'usection');
1.417     albertel 1007:     if (defined($cached)) { return $result; }
1.70      www      1008:     $courseid=~s/\_/\//g;
                   1009:     $courseid=~s/^(\w)/\/$1/;
1.191     harris41 1010:     foreach (split(/\&/,&reply('dump:'.$udom.':'.$unam.':roles',
                   1011:                         &homeserver($unam,$udom)))) {
1.70      www      1012:         my ($key,$value)=split(/\=/,$_);
                   1013:         $key=&unescape($key);
1.479     albertel 1014:         if ($key=~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/) {
1.70      www      1015:             my $section=$1;
                   1016:             if ($key eq $courseid.'_st') { $section=''; }
                   1017: 	    my ($dummy,$end,$start)=split(/\_/,&unescape($value));
                   1018:             my $now=time;
                   1019:             my $notactive=0;
                   1020:             if ($start) {
                   1021: 		if ($now<$start) { $notactive=1; }
                   1022:             }
                   1023:             if ($end) {
                   1024:                 if ($now>$end) { $notactive=1; }
                   1025:             } 
1.416     albertel 1026:             unless ($notactive) {
1.425     albertel 1027: 		return &do_cache(\%usectioncache,$hashid,$section,'usection');
1.416     albertel 1028: 	    }
1.70      www      1029:         }
1.191     harris41 1030:     }
1.425     albertel 1031:     return &do_cache(\%usectioncache,$hashid,'-1','usection');
1.70      www      1032: }
                   1033: 
                   1034: # ------------------------------------- Read an entry from a user's environment
                   1035: 
                   1036: sub userenvironment {
                   1037:     my ($udom,$unam,@what)=@_;
                   1038:     my %returnhash=();
                   1039:     my @answer=split(/\&/,
                   1040:                 &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
                   1041:                       &homeserver($unam,$udom)));
                   1042:     my $i;
                   1043:     for ($i=0;$i<=$#what;$i++) {
                   1044: 	$returnhash{$what[$i]}=&unescape($answer[$i]);
                   1045:     }
                   1046:     return %returnhash;
1.1       albertel 1047: }
                   1048: 
1.263     www      1049: # -------------------------------------------------------------------- New chat
                   1050: 
                   1051: sub chatsend {
                   1052:     my ($newentry,$anon)=@_;
                   1053:     my $cnum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                   1054:     my $cdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                   1055:     my $chome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
                   1056:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
                   1057: 	   &escape($ENV{'user.domain'}.':'.$ENV{'user.name'}.':'.$anon.':'.
                   1058: 		   &escape($newentry)),$chome);
1.292     www      1059: }
                   1060: 
                   1061: # ------------------------------------------ Find current version of a resource
                   1062: 
                   1063: sub getversion {
                   1064:     my $fname=&clutter(shift);
                   1065:     unless ($fname=~/^\/res\//) { return -1; }
                   1066:     return &currentversion(&filelocation('',$fname));
                   1067: }
                   1068: 
                   1069: sub currentversion {
                   1070:     my $fname=shift;
1.440     www      1071:     my ($result,$cached)=&is_cached(\%resversioncache,$fname,'resversion',600);
                   1072:     if (defined($cached)) { return $result; }
1.292     www      1073:     my $author=$fname;
                   1074:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1075:     my ($udom,$uname)=split(/\//,$author);
                   1076:     my $home=homeserver($uname,$udom);
                   1077:     if ($home eq 'no_host') { 
                   1078:         return -1; 
                   1079:     }
                   1080:     my $answer=reply("currentversion:$fname",$home);
                   1081:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1082: 	return -1;
                   1083:     }
1.440     www      1084:     return &do_cache(\%resversioncache,$fname,$answer,'resversion');
1.263     www      1085: }
                   1086: 
1.1       albertel 1087: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      1088: 
1.1       albertel 1089: sub subscribe {
                   1090:     my $fname=shift;
1.312     www      1091:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 1092:     $fname=~s/[\n\r]//g;
1.1       albertel 1093:     my $author=$fname;
                   1094:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1095:     my ($udom,$uname)=split(/\//,$author);
                   1096:     my $home=homeserver($uname,$udom);
1.335     albertel 1097:     if ($home eq 'no_host') {
                   1098:         return 'not_found';
1.1       albertel 1099:     }
                   1100:     my $answer=reply("sub:$fname",$home);
1.64      www      1101:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1102: 	$answer.=' by '.$home;
                   1103:     }
1.1       albertel 1104:     return $answer;
                   1105: }
                   1106:     
1.8       www      1107: # -------------------------------------------------------------- Replicate file
                   1108: 
                   1109: sub repcopy {
                   1110:     my $filename=shift;
1.23      www      1111:     $filename=~s/\/+/\//g;
1.538     albertel 1112:     if ($filename=~m|^/home/httpd/html/adm/|) { return OK; }
                   1113:     if ($filename=~m|^/home/httpd/html/lonUsers/|) { return OK; }
                   1114:     if ($filename=~m|^/home/httpd/html/userfiles/| or
                   1115: 	$filename=~m|^/*uploaded/|) { 
                   1116: 	return &repcopy_userfile($filename);
                   1117:     }
1.532     albertel 1118:     $filename=~s/[\n\r]//g;
1.8       www      1119:     my $transname="$filename.in.transfer";
1.17      www      1120:     if ((-e $filename) || (-e $transname)) { return OK; }
1.8       www      1121:     my $remoteurl=subscribe($filename);
1.64      www      1122:     if ($remoteurl =~ /^con_lost by/) {
                   1123: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.8       www      1124:            return HTTP_SERVICE_UNAVAILABLE;
                   1125:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 1126: 	   #&logthis("Subscribe returned not_found: $filename");
1.8       www      1127: 	   return HTTP_NOT_FOUND;
1.64      www      1128:     } elsif ($remoteurl =~ /^rejected by/) {
                   1129: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.8       www      1130:            return FORBIDDEN;
1.20      www      1131:     } elsif ($remoteurl eq 'directory') {
                   1132:            return OK;
1.8       www      1133:     } else {
1.290     www      1134:         my $author=$filename;
                   1135:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1136:         my ($udom,$uname)=split(/\//,$author);
                   1137:         my $home=homeserver($uname,$udom);
                   1138:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      1139:            my @parts=split(/\//,$filename);
                   1140:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   1141:            if ($path ne "$perlvar{'lonDocRoot'}/res") {
                   1142:                &logthis("Malconfiguration for replication: $filename");
                   1143: 	       return HTTP_BAD_REQUEST;
                   1144:            }
                   1145:            my $count;
                   1146:            for ($count=5;$count<$#parts;$count++) {
                   1147:                $path.="/$parts[$count]";
                   1148:                if ((-e $path)!=1) {
                   1149: 		   mkdir($path,0777);
                   1150:                }
                   1151:            }
                   1152:            my $ua=new LWP::UserAgent;
                   1153:            my $request=new HTTP::Request('GET',"$remoteurl");
                   1154:            my $response=$ua->request($request,$transname);
                   1155:            if ($response->is_error()) {
                   1156: 	       unlink($transname);
                   1157:                my $message=$response->status_line;
1.12      www      1158:                &logthis("<font color=blue>WARNING:"
                   1159:                        ." LWP get: $message: $filename</font>");
1.8       www      1160:                return HTTP_SERVICE_UNAVAILABLE;
                   1161:            } else {
1.16      www      1162: 	       if ($remoteurl!~/\.meta$/) {
                   1163:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
                   1164:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
                   1165:                   if ($mresponse->is_error()) {
                   1166: 		      unlink($filename.'.meta');
                   1167:                       &logthis(
                   1168:                      "<font color=yellow>INFO: No metadata: $filename</font>");
                   1169:                   }
                   1170: 	       }
1.8       www      1171:                rename($transname,$filename);
                   1172:                return OK;
                   1173:            }
1.290     www      1174:        }
1.8       www      1175:     }
1.330     www      1176: }
                   1177: 
                   1178: # ------------------------------------------------ Get server side include body
                   1179: sub ssi_body {
1.381     albertel 1180:     my ($filelink,%form)=@_;
1.330     www      1181:     my $output=($filelink=~/^http\:/?&externalssi($filelink):
1.381     albertel 1182:                                      &ssi($filelink,%form));
1.525     albertel 1183:     $output=~
                   1184:             s/\/\/ BEGIN LON\-CAPA Internal.+\/\/ END LON\-CAPA Internal\s//gs;
1.451     albertel 1185:     $output=~s/^.*?\<body[^\>]*\>//si;
                   1186:     $output=~s/(.*)\<\/body\s*\>.*?$/$1/si;
1.330     www      1187:     return $output;
1.8       www      1188: }
                   1189: 
1.15      www      1190: # --------------------------------------------------------- Server Side Include
                   1191: 
                   1192: sub ssi {
                   1193: 
1.23      www      1194:     my ($fn,%form)=@_;
1.15      www      1195: 
                   1196:     my $ua=new LWP::UserAgent;
1.23      www      1197:     
                   1198:     my $request;
                   1199:     
                   1200:     if (%form) {
                   1201:       $request=new HTTP::Request('POST',"http://".$ENV{'HTTP_HOST'}.$fn);
1.201     albertel 1202:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1.23      www      1203:     } else {
                   1204:       $request=new HTTP::Request('GET',"http://".$ENV{'HTTP_HOST'}.$fn);
                   1205:     }
                   1206: 
1.15      www      1207:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
                   1208:     my $response=$ua->request($request);
                   1209: 
1.324     www      1210:     return $response->content;
                   1211: }
                   1212: 
                   1213: sub externalssi {
                   1214:     my ($url)=@_;
                   1215:     my $ua=new LWP::UserAgent;
                   1216:     my $request=new HTTP::Request('GET',$url);
                   1217:     my $response=$ua->request($request);
1.15      www      1218:     return $response->content;
                   1219: }
1.254     www      1220: 
1.492     albertel 1221: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   1222: 
                   1223: sub allowuploaded {
                   1224:     my ($srcurl,$url)=@_;
                   1225:     $url=&clutter(&declutter($url));
                   1226:     my $dir=$url;
                   1227:     $dir=~s/\/[^\/]+$//;
                   1228:     my %httpref=();
                   1229:     my $httpurl=&hreflocation('',$url);
                   1230:     $httpref{'httpref.'.$httpurl}=$srcurl;
                   1231:     &Apache::lonnet::appenv(%httpref);
1.254     www      1232: }
1.477     raeburn  1233: 
1.478     albertel 1234: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
                   1235: # input: action, courseID, current domain, home server for course, intended
                   1236: #        path to file, source of file.
1.485     raeburn  1237: # output: url to file (if action was uploaddoc), 
                   1238: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  1239: #
1.478     albertel 1240: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   1241: # course.
1.477     raeburn  1242: #
1.478     albertel 1243: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1244: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   1245: #          course's home server.
1.477     raeburn  1246: #
1.478     albertel 1247: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   1248: #          be copied from $source (current location) to 
                   1249: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1250: #         and will then be copied to
                   1251: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   1252: #         course's home server.
1.485     raeburn  1253: #
1.481     raeburn  1254: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.485     raeburn  1255: #         will be retrived from $ENV{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  1256: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1257: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   1258: #         in course's home server.
                   1259: 
1.477     raeburn  1260: 
                   1261: sub process_coursefile {
                   1262:     my ($action,$docuname,$docudom,$docuhome,$file,$source)=@_;
                   1263:     my $fetchresult;
                   1264:     if ($action eq 'propagate') {
                   1265:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file
                   1266:                             ,$docuhome);
1.481     raeburn  1267:     } else {
1.477     raeburn  1268:         my $fetchresult = '';
                   1269:         my $fpath = '';
                   1270:         my $fname = $file;
1.478     albertel 1271:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  1272:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   1273:         my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   1274:         unless ($fpath eq '') {
1.478     albertel 1275:             my @parts=split('/',$fpath);
1.477     raeburn  1276:             foreach my $part (@parts) {
                   1277:                 $filepath.= '/'.$part;
                   1278:                 if ((-e $filepath)!=1) {
                   1279:                     mkdir($filepath,0777);
                   1280:                 }
                   1281:             }
                   1282:         }
1.481     raeburn  1283:         if ($action eq 'copy') {
                   1284:             if ($source eq '') {
                   1285:                 $fetchresult = 'no source file';
                   1286:                 return $fetchresult;
                   1287:             } else {
                   1288:                 my $destination = $filepath.'/'.$fname;
                   1289:                 rename($source,$destination);
                   1290:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   1291:                                  $docuhome);
                   1292:             }
                   1293:         } elsif ($action eq 'uploaddoc') {
                   1294:             open(my $fh,'>'.$filepath.'/'.$fname);
                   1295:             print $fh $ENV{'form.'.$source};
                   1296:             close($fh);
1.477     raeburn  1297:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   1298:                                  $docuhome);
1.481     raeburn  1299:             if ($fetchresult eq 'ok') {
                   1300:                 return '/uploaded/'.$fpath.'/'.$fname;
                   1301:             } else {
                   1302:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   1303:                         ' to host '.$docuhome.': '.$fetchresult);
                   1304:                 return '/adm/notfound.html';
                   1305:             }
1.477     raeburn  1306:         }
                   1307:     }
1.485     raeburn  1308:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  1309:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   1310:              ' to host '.$docuhome.': '.$fetchresult);
                   1311:     }
                   1312:     return $fetchresult;
                   1313: }
                   1314: 
1.257     www      1315: # --------------- Take an uploaded file and put it into the userfiles directory
1.259     www      1316: # input: name of form element, coursedoc=1 means this is for the course
1.257     www      1317: # output: url of file in userspace
                   1318: 
1.531     albertel 1319: sub clean_filename {
                   1320:     my ($fname)=@_;
1.315     www      1321: # Replace Windows backslashes by forward slashes
1.257     www      1322:     $fname=~s/\\/\//g;
1.315     www      1323: # Get rid of everything but the actual filename
1.257     www      1324:     $fname=~s/^.*\/([^\/]+)$/$1/;
1.315     www      1325: # Replace spaces by underscores
                   1326:     $fname=~s/\s+/\_/g;
                   1327: # Replace all other weird characters by nothing
1.317     www      1328:     $fname=~s/[^\w\.\-]//g;
1.540     albertel 1329: # Replace all .\d. sequences with _\d. so they no longer look like version
                   1330: # numbers
                   1331:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531     albertel 1332:     return $fname;
                   1333: }
                   1334: 
                   1335: sub userfileupload {
                   1336:     my ($formname,$coursedoc,$subdir)=@_;
                   1337:     if (!defined($subdir)) { $subdir='unknown'; }
                   1338:     my $fname=$ENV{'form.'.$formname.'.filename'};
                   1339:     $fname=&clean_filename($fname);
1.315     www      1340: # See if there is anything left
1.257     www      1341:     unless ($fname) { return 'error: no uploaded file'; }
1.477     raeburn  1342:     chop($ENV{'form.'.$formname});
1.523     raeburn  1343:     if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
                   1344:         my $now = time;
                   1345:         my $filepath = 'tmp/helprequests/'.$now;
                   1346:         my @parts=split(/\//,$filepath);
                   1347:         my $fullpath = $perlvar{'lonDaemons'};
                   1348:         for (my $i=0;$i<@parts;$i++) {
                   1349:             $fullpath .= '/'.$parts[$i];
                   1350:             if ((-e $fullpath)!=1) {
                   1351:                 mkdir($fullpath,0777);
                   1352:             }
                   1353:         }
                   1354:         open(my $fh,'>'.$fullpath.'/'.$fname);
                   1355:         print $fh $ENV{'form.'.$formname};
                   1356:         close($fh);
                   1357:         return $fullpath.'/'.$fname; 
                   1358:     }
1.258     www      1359: # Create the directory if not present
1.259     www      1360:     my $docuname='';
                   1361:     my $docudom='';
                   1362:     my $docuhome='';
1.493     albertel 1363:     $fname="$subdir/$fname";
1.259     www      1364:     if ($coursedoc) {
                   1365: 	$docuname=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                   1366: 	$docudom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                   1367: 	$docuhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1.481     raeburn  1368:         if ($ENV{'form.folder'} =~ m/^default/) {
1.485     raeburn  1369:             return &finishuserfileupload($docuname,$docudom,$docuhome,$formname,$fname);
1.481     raeburn  1370:         } else {
                   1371:             $fname=$ENV{'form.folder'}.'/'.$fname;
1.485     raeburn  1372:             return &process_coursefile('uploaddoc',$docuname,$docudom,$docuhome,$fname,$formname);
1.481     raeburn  1373:         }
1.259     www      1374:     } else {
                   1375:         $docuname=$ENV{'user.name'};
                   1376:         $docudom=$ENV{'user.domain'};
                   1377:         $docuhome=$ENV{'user.home'};
1.485     raeburn  1378:         return &finishuserfileupload($docuname,$docudom,$docuhome,$formname,$fname);
1.259     www      1379:     }
1.271     www      1380: }
                   1381: 
                   1382: sub finishuserfileupload {
1.477     raeburn  1383:     my ($docuname,$docudom,$docuhome,$formname,$fname)=@_;
                   1384:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      1385:     my $filepath=$perlvar{'lonDocRoot'};
1.494     albertel 1386:     my ($fnamepath,$file);
                   1387:     $file=$fname;
                   1388:     if ($fname=~m|/|) {
                   1389:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   1390: 	$path.=$fnamepath.'/';
                   1391:     }
1.259     www      1392:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      1393:     my $count;
                   1394:     for ($count=4;$count<=$#parts;$count++) {
                   1395:         $filepath.="/$parts[$count]";
                   1396:         if ((-e $filepath)!=1) {
                   1397: 	    mkdir($filepath,0777);
                   1398:         }
                   1399:     }
                   1400: # Save the file
                   1401:     {
1.500     albertel 1402: 	#&Apache::lonnet::logthis("Saving to $filepath $file");
1.494     albertel 1403:        open(my $fh,'>'.$filepath.'/'.$file);
1.477     raeburn  1404:        print $fh $ENV{'form.'.$formname};
                   1405:        close($fh);
1.258     www      1406:     }
1.259     www      1407: # Notify homeserver to grep it
                   1408: #
1.494     albertel 1409:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      1410:     if ($fetchresult eq 'ok') {
1.259     www      1411: #
1.258     www      1412: # Return the URL to it
1.494     albertel 1413:         return '/uploaded/'.$path.$file;
1.263     www      1414:     } else {
1.494     albertel 1415:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   1416: 		 ': '.$fetchresult);
1.263     www      1417:         return '/adm/notfound.html';
                   1418:     }    
1.493     albertel 1419: }
                   1420: 
                   1421: sub removeuploadedurl {
                   1422:     my ($url)=@_;
                   1423:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
                   1424:     return &Apache::lonnet::removeuserfile($uname,$udom,$fname);
1.490     albertel 1425: }
                   1426: 
                   1427: sub removeuserfile {
                   1428:     my ($docuname,$docudom,$fname)=@_;
                   1429:     my $home=&homeserver($docuname,$docudom);
                   1430:     return &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.257     www      1431: }
1.15      www      1432: 
1.530     albertel 1433: sub mkdiruserfile {
                   1434:     my ($docuname,$docudom,$dir)=@_;
                   1435:     my $home=&homeserver($docuname,$docudom);
                   1436:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   1437: }
                   1438: 
1.531     albertel 1439: sub renameuserfile {
                   1440:     my ($docuname,$docudom,$old,$new)=@_;
                   1441:     my $home=&homeserver($docuname,$docudom);
                   1442:     return &reply("renameuserfile:$docudom:$docuname:".&escape("$old").':'.
                   1443: 		  &escape("$new"),$home);
                   1444: }
                   1445: 
1.14      www      1446: # ------------------------------------------------------------------------- Log
                   1447: 
                   1448: sub log {
                   1449:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      1450:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      1451: }
                   1452: 
                   1453: # ------------------------------------------------------------------ Course Log
1.352     www      1454: #
                   1455: # This routine flushes several buffers of non-mission-critical nature
                   1456: #
1.157     www      1457: 
                   1458: sub flushcourselogs {
1.352     www      1459:     &logthis('Flushing log buffers');
                   1460: #
                   1461: # course logs
                   1462: # This is a log of all transactions in a course, which can be used
                   1463: # for data mining purposes
                   1464: #
                   1465: # It also collects the courseid database, which lists last transaction
                   1466: # times and course titles for all courseids
                   1467: #
                   1468:     my %courseidbuffer=();
1.191     harris41 1469:     foreach (keys %courselogs) {
1.157     www      1470:         my $crsid=$_;
1.352     www      1471:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      1472: 		          &escape($courselogs{$crsid}),
                   1473: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      1474: 	    delete $courselogs{$crsid};
                   1475:         } else {
                   1476:             &logthis('Failed to flush log buffer for '.$crsid);
                   1477:             if (length($courselogs{$crsid})>40000) {
                   1478:                &logthis("<font color=blue>WARNING: Buffer for ".$crsid.
                   1479:                         " exceeded maximum size, deleting.</font>");
                   1480:                delete $courselogs{$crsid};
                   1481:             }
1.352     www      1482:         }
                   1483:         if ($courseidbuffer{$coursehombuf{$crsid}}) {
                   1484:            $courseidbuffer{$coursehombuf{$crsid}}.='&'.
1.516     raeburn  1485: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
                   1486:                          '='.&escape($courseinstcodebuf{$crsid});
1.352     www      1487:         } else {
                   1488:            $courseidbuffer{$coursehombuf{$crsid}}=
1.516     raeburn  1489: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
                   1490:                          '='.&escape($courseinstcodebuf{$crsid});
1.352     www      1491:         }    
1.191     harris41 1492:     }
1.352     www      1493: #
                   1494: # Write course id database (reverse lookup) to homeserver of courses 
                   1495: # Is used in pickcourse
                   1496: #
                   1497:     foreach (keys %courseidbuffer) {
1.353     www      1498:         &courseidput($hostdom{$_},$courseidbuffer{$_},$_);
1.352     www      1499:     }
                   1500: #
                   1501: # File accesses
                   1502: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   1503: #
1.449     matthew  1504:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  1505:         if ($entry =~ /___count$/) {
                   1506:             my ($dom,$name);
                   1507:             ($dom,$name,undef)=($entry=~m:___(\w+)/(\w+)/(.*)___count$:);
                   1508:             if (! defined($dom) || $dom eq '' || 
                   1509:                 ! defined($name) || $name eq '') {
                   1510:                 my $cid = $ENV{'request.course.id'};
                   1511:                 $dom  = $ENV{'request.'.$cid.'.domain'};
                   1512:                 $name = $ENV{'request.'.$cid.'.num'};
                   1513:             }
1.450     matthew  1514:             my $value = $accesshash{$entry};
                   1515:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   1516:             my %temphash=($url => $value);
1.449     matthew  1517:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   1518:             if ($result eq 'ok') {
                   1519:                 delete $accesshash{$entry};
                   1520:             } elsif ($result eq 'unknown_cmd') {
                   1521:                 # Target server has old code running on it.
1.450     matthew  1522:                 my %temphash=($entry => $value);
1.449     matthew  1523:                 if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   1524:                     delete $accesshash{$entry};
                   1525:                 }
                   1526:             }
                   1527:         } else {
1.458     matthew  1528:             my ($dom,$name) = ($entry=~m:___(\w+)/(\w+)/(.*)___(\w+)$:);
1.450     matthew  1529:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  1530:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   1531:                 delete $accesshash{$entry};
                   1532:             }
1.185     www      1533:         }
1.191     harris41 1534:     }
1.352     www      1535: #
                   1536: # Roles
                   1537: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   1538: #
1.349     www      1539:     foreach (keys %userrolehash) {
                   1540:         my $entry=$_;
1.351     www      1541:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      1542: 	    split(/\:/,$entry);
                   1543:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      1544:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      1545:                 $rudom,$runame) eq 'ok') {
                   1546: 	    delete $userrolehash{$entry};
                   1547:         }
                   1548:     }
1.186     www      1549:     $dumpcount++;
1.157     www      1550: }
                   1551: 
                   1552: sub courselog {
                   1553:     my $what=shift;
1.158     www      1554:     $what=time.':'.$what;
1.157     www      1555:     unless ($ENV{'request.course.id'}) { return ''; }
1.188     www      1556:     $coursedombuf{$ENV{'request.course.id'}}=
1.352     www      1557:        $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                   1558:     $coursenumbuf{$ENV{'request.course.id'}}=
1.188     www      1559:        $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                   1560:     $coursehombuf{$ENV{'request.course.id'}}=
                   1561:        $ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1.352     www      1562:     $coursedescrbuf{$ENV{'request.course.id'}}=
                   1563:        $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1.516     raeburn  1564:     $courseinstcodebuf{$ENV{'request.course.id'}}=
                   1565:        $ENV{'course.'.$ENV{'request.course.id'}.'.internal.coursecode'};
1.157     www      1566:     if (defined $courselogs{$ENV{'request.course.id'}}) {
                   1567: 	$courselogs{$ENV{'request.course.id'}}.='&'.$what;
                   1568:     } else {
                   1569: 	$courselogs{$ENV{'request.course.id'}}.=$what;
                   1570:     }
1.458     matthew  1571:     if (length($courselogs{$ENV{'request.course.id'}})>4048) {
1.157     www      1572: 	&flushcourselogs();
                   1573:     }
1.158     www      1574: }
                   1575: 
                   1576: sub courseacclog {
                   1577:     my $fnsymb=shift;
                   1578:     unless ($ENV{'request.course.id'}) { return ''; }
                   1579:     my $what=$fnsymb.':'.$ENV{'user.name'}.':'.$ENV{'user.domain'};
1.408     www      1580:     if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|page)$/) {
1.187     www      1581:         $what.=':POST';
1.191     harris41 1582: 	foreach (keys %ENV) {
1.158     www      1583:             if ($_=~/^form\.(.*)/) {
                   1584: 		$what.=':'.$1.'='.$ENV{$_};
                   1585:             }
1.191     harris41 1586:         }
1.158     www      1587:     }
                   1588:     &courselog($what);
1.149     www      1589: }
                   1590: 
1.185     www      1591: sub countacc {
                   1592:     my $url=&declutter(shift);
1.458     matthew  1593:     return if (! defined($url) || $url eq '');
1.185     www      1594:     unless ($ENV{'request.course.id'}) { return ''; }
                   1595:     $accesshash{$ENV{'request.course.id'}.'___'.$url.'___course'}=1;
1.281     www      1596:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  1597:     $accesshash{$key}++;
1.185     www      1598: }
1.349     www      1599: 
1.361     www      1600: sub linklog {
                   1601:     my ($from,$to)=@_;
                   1602:     $from=&declutter($from);
                   1603:     $to=&declutter($to);
                   1604:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   1605:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   1606: }
                   1607:   
1.349     www      1608: sub userrolelog {
                   1609:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
                   1610:     if (($trole=~/^ca/) || ($trole=~/^in/) || 
                   1611:         ($trole=~/^cc/) || ($trole=~/^ep/) ||
1.469     www      1612:         ($trole=~/^cr/) || ($trole=~/^ta/)) {
1.350     www      1613:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   1614:        $userrolehash
                   1615:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      1616:                     =$tend.':'.$tstart;
                   1617:    }
1.351     www      1618: }
                   1619: 
                   1620: sub get_course_adv_roles {
                   1621:     my $cid=shift;
                   1622:     $cid=$ENV{'request.course.id'} unless (defined($cid));
                   1623:     my %coursehash=&coursedescription($cid);
1.470     www      1624:     my %nothide=();
                   1625:     foreach (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   1626: 	$nothide{join(':',split(/[\@\:]/,$_))}=1;
                   1627:     }
1.351     www      1628:     my %returnhash=();
                   1629:     my %dumphash=
                   1630:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   1631:     my $now=time;
                   1632:     foreach (keys %dumphash) {
                   1633: 	my ($tend,$tstart)=split(/\:/,$dumphash{$_});
                   1634:         if (($tstart) && ($tstart<0)) { next; }
                   1635:         if (($tend) && ($tend<$now)) { next; }
                   1636:         if (($tstart) && ($now<$tstart)) { next; }
                   1637:         my ($role,$username,$domain,$section)=split(/\:/,$_);
1.470     www      1638: 	if ((&privileged($username,$domain)) && 
                   1639: 	    (!$nothide{$username.':'.$domain})) { next; }
1.351     www      1640:         my $key=&plaintext($role);
                   1641:         if ($section) { $key.=' (Sec/Grp '.$section.')'; }
                   1642:         if ($returnhash{$key}) {
                   1643: 	    $returnhash{$key}.=','.$username.':'.$domain;
                   1644:         } else {
                   1645:             $returnhash{$key}=$username.':'.$domain;
                   1646:         }
1.400     www      1647:      }
                   1648:     return %returnhash;
                   1649: }
                   1650: 
                   1651: sub get_my_roles {
                   1652:     my ($uname,$udom)=@_;
                   1653:     unless (defined($uname)) { $uname=$ENV{'user.name'}; }
                   1654:     unless (defined($udom)) { $udom=$ENV{'user.domain'}; }
                   1655:     my %dumphash=
                   1656:             &dump('nohist_userroles',$udom,$uname);
                   1657:     my %returnhash=();
                   1658:     my $now=time;
                   1659:     foreach (keys %dumphash) {
                   1660: 	my ($tend,$tstart)=split(/\:/,$dumphash{$_});
                   1661:         if (($tstart) && ($tstart<0)) { next; }
                   1662:         if (($tend) && ($tend<$now)) { next; }
                   1663:         if (($tstart) && ($now<$tstart)) { next; }
                   1664:         my ($role,$username,$domain,$section)=split(/\:/,$_);
                   1665: 	$returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
1.373     www      1666:      }
                   1667:     return %returnhash;
1.399     www      1668: }
                   1669: 
                   1670: # ----------------------------------------------------- Frontpage Announcements
                   1671: #
                   1672: #
                   1673: 
                   1674: sub postannounce {
                   1675:     my ($server,$text)=@_;
                   1676:     unless (&allowed('psa',$hostdom{$server})) { return 'refused'; }
                   1677:     unless ($text=~/\w/) { $text=''; }
                   1678:     return &reply('setannounce:'.&escape($text),$server);
                   1679: }
                   1680: 
                   1681: sub getannounce {
1.448     albertel 1682: 
                   1683:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      1684: 	my $announcement='';
                   1685: 	while (<$fh>) { $announcement .=$_; }
1.448     albertel 1686: 	close($fh);
1.399     www      1687: 	if ($announcement=~/\w/) { 
                   1688: 	    return 
                   1689:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 1690:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      1691: 	} else {
                   1692: 	    return '';
                   1693: 	}
                   1694:     } else {
                   1695: 	return '';
                   1696:     }
1.351     www      1697: }
1.353     www      1698: 
                   1699: # ---------------------------------------------------------- Course ID routines
                   1700: # Deal with domain's nohist_courseid.db files
                   1701: #
                   1702: 
                   1703: sub courseidput {
                   1704:     my ($domain,$what,$coursehome)=@_;
                   1705:     return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   1706: }
                   1707: 
                   1708: sub courseiddump {
1.511     raeburn  1709:     my ($domfilter,$descfilter,$sincefilter,$hostidflag,$hostidref)=@_;
1.353     www      1710:     my %returnhash=();
1.355     www      1711:     unless ($domfilter) { $domfilter=''; }
1.353     www      1712:     foreach my $tryserver (keys %libserv) {
1.511     raeburn  1713:         if ( ($hostidflag == 1 && grep/^$tryserver$/,@{$hostidref}) || (!defined($hostidflag)) ) {
1.506     raeburn  1714: 	    if ((!$domfilter) || ($hostdom{$tryserver} eq $domfilter)) {
                   1715: 	        foreach (
                   1716:                  split(/\&/,&reply('courseiddump:'.$hostdom{$tryserver}.':'.
1.354     www      1717: 			       $sincefilter.':'.&escape($descfilter),
                   1718:                                $tryserver))) {
1.506     raeburn  1719: 		    my ($key,$value)=split(/\=/,$_);
                   1720:                     if (($key) && ($value)) {
1.516     raeburn  1721: 		        $returnhash{&unescape($key)}=$value;
1.506     raeburn  1722:                     }
1.353     www      1723:                 }
                   1724:             }
                   1725:         }
                   1726:     }
                   1727:     return %returnhash;
                   1728: }
                   1729: 
                   1730: #
1.149     www      1731: # ----------------------------------------------------------- Check out an item
                   1732: 
1.504     albertel 1733: sub get_first_access {
                   1734:     my ($type,$argsymb)=@_;
                   1735:     my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1736:     if ($argsymb) { $symb=$argsymb; }
                   1737:     my ($map,$id,$res)=&decode_symb($symb);
                   1738:     if ($type eq 'map') { $res=$map; }
                   1739:     my %times=&get('firstaccesstimes',[$res],$udom,$uname);
                   1740:     return $times{$res};
                   1741: }
                   1742: 
                   1743: sub set_first_access {
                   1744:     my ($type)=@_;
                   1745:     my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1746:     my ($map,$id,$res)=&decode_symb($symb);
                   1747:     if ($type eq 'map') { $res=$map; }
1.505     albertel 1748:     my $firstaccess=&get_first_access($type);
                   1749:     if (!$firstaccess) {
                   1750: 	return &put('firstaccesstimes',{$res=>time},$udom,$uname);
                   1751:     }
                   1752:     return 'already_set';
1.504     albertel 1753: }
                   1754: 
1.149     www      1755: sub checkout {
                   1756:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
                   1757:     my $now=time;
                   1758:     my $lonhost=$perlvar{'lonHostID'};
                   1759:     my $infostr=&escape(
1.234     www      1760:                  'CHECKOUTTOKEN&'.
1.149     www      1761:                  $tuname.'&'.
                   1762:                  $tudom.'&'.
                   1763:                  $tcrsid.'&'.
                   1764:                  $symb.'&'.
                   1765: 		 $now.'&'.$ENV{'REMOTE_ADDR'});
                   1766:     my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151     www      1767:     if ($token=~/^error\:/) { 
                   1768:         &logthis("<font color=blue>WARNING: ".
                   1769:                 "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
                   1770:                  "</font>");
                   1771:         return ''; 
                   1772:     }
                   1773: 
1.149     www      1774:     $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
                   1775:     $token=~tr/a-z/A-Z/;
                   1776: 
1.153     www      1777:     my %infohash=('resource.0.outtoken' => $token,
                   1778:                   'resource.0.checkouttime' => $now,
                   1779:                   'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149     www      1780: 
                   1781:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   1782:        return '';
1.151     www      1783:     } else {
                   1784:         &logthis("<font color=blue>WARNING: ".
                   1785:                 "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
                   1786:                  "</font>");
1.149     www      1787:     }    
                   1788: 
                   1789:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   1790:                          &escape('Checkout '.$infostr.' - '.
                   1791:                                                  $token)) ne 'ok') {
                   1792: 	return '';
1.151     www      1793:     } else {
                   1794:         &logthis("<font color=blue>WARNING: ".
                   1795:                 "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
                   1796:                  "</font>");
1.149     www      1797:     }
1.151     www      1798:     return $token;
1.149     www      1799: }
                   1800: 
                   1801: # ------------------------------------------------------------ Check in an item
                   1802: 
                   1803: sub checkin {
                   1804:     my $token=shift;
1.150     www      1805:     my $now=time;
                   1806:     my ($ta,$tb,$lonhost)=split(/\*/,$token);
                   1807:     $lonhost=~tr/A-Z/a-z/;
                   1808:     my $dtoken=$ta.'_'.$hostip{$lonhost}.'_'.$tb;
                   1809:     $dtoken=~s/\W/\_/g;
1.234     www      1810:     my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150     www      1811:                  split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
                   1812: 
1.154     www      1813:     unless (($tuname) && ($tudom)) {
                   1814:         &logthis('Check in '.$token.' ('.$dtoken.') failed');
                   1815:         return '';
                   1816:     }
                   1817:     
                   1818:     unless (&allowed('mgr',$tcrsid)) {
                   1819:         &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
                   1820:                  $ENV{'user.name'}.' - '.$ENV{'user.domain'});
                   1821:         return '';
                   1822:     }
                   1823: 
1.153     www      1824:     my %infohash=('resource.0.intoken' => $token,
                   1825:                   'resource.0.checkintime' => $now,
                   1826:                   'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150     www      1827: 
                   1828:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   1829:        return '';
                   1830:     }    
                   1831: 
                   1832:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   1833:                          &escape('Checkin - '.$token)) ne 'ok') {
                   1834: 	return '';
                   1835:     }
                   1836: 
                   1837:     return ($symb,$tuname,$tudom,$tcrsid);    
1.110     www      1838: }
                   1839: 
                   1840: # --------------------------------------------- Set Expire Date for Spreadsheet
                   1841: 
                   1842: sub expirespread {
                   1843:     my ($uname,$udom,$stype,$usymb)=@_;
                   1844:     my $cid=$ENV{'request.course.id'}; 
                   1845:     if ($cid) {
                   1846:        my $now=time;
                   1847:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
                   1848:        return &reply('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
                   1849:                             $ENV{'course.'.$cid.'.num'}.
                   1850: 	        	    ':nohist_expirationdates:'.
                   1851:                             &escape($key).'='.$now,
                   1852:                             $ENV{'course.'.$cid.'.home'})
                   1853:     }
                   1854:     return 'ok';
1.14      www      1855: }
                   1856: 
1.109     www      1857: # ----------------------------------------------------- Devalidate Spreadsheets
                   1858: 
                   1859: sub devalidate {
1.325     www      1860:     my ($symb,$uname,$udom)=@_;
1.109     www      1861:     my $cid=$ENV{'request.course.id'}; 
                   1862:     if ($cid) {
1.391     matthew  1863:         # delete the stored spreadsheets for
                   1864:         # - the student level sheet of this user in course's homespace
                   1865:         # - the assessment level sheet for this resource 
                   1866:         #   for this user in user's homespace
1.325     www      1867: 	my $key=$uname.':'.$udom.':';
1.109     www      1868:         my $status=
1.299     matthew  1869: 	    &del('nohist_calculatedsheets',
1.391     matthew  1870: 		 [$key.'studentcalc:'],
1.133     albertel 1871: 		 $ENV{'course.'.$cid.'.domain'},
                   1872: 		 $ENV{'course.'.$cid.'.num'})
                   1873: 		.' '.
                   1874: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  1875: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      1876:         unless ($status eq 'ok ok') {
                   1877:            &logthis('Could not devalidate spreadsheet '.
1.325     www      1878:                     $uname.' at '.$udom.' for '.
1.109     www      1879: 		    $symb.': '.$status);
1.133     albertel 1880:         }
1.109     www      1881:     }
                   1882: }
                   1883: 
1.265     albertel 1884: sub get_scalar {
                   1885:     my ($string,$end) = @_;
                   1886:     my $value;
                   1887:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   1888: 	$value = $1;
                   1889:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   1890: 	$value = $1;
                   1891:     }
                   1892:     return &unescape($value);
                   1893: }
                   1894: 
                   1895: sub array2str {
                   1896:   my (@array) = @_;
                   1897:   my $result=&arrayref2str(\@array);
                   1898:   $result=~s/^__ARRAY_REF__//;
                   1899:   $result=~s/__END_ARRAY_REF__$//;
                   1900:   return $result;
                   1901: }
                   1902: 
1.204     albertel 1903: sub arrayref2str {
                   1904:   my ($arrayref) = @_;
1.265     albertel 1905:   my $result='__ARRAY_REF__';
1.204     albertel 1906:   foreach my $elem (@$arrayref) {
1.265     albertel 1907:     if(ref($elem) eq 'ARRAY') {
                   1908:       $result.=&arrayref2str($elem).'&';
                   1909:     } elsif(ref($elem) eq 'HASH') {
                   1910:       $result.=&hashref2str($elem).'&';
                   1911:     } elsif(ref($elem)) {
                   1912:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 1913:     } else {
                   1914:       $result.=&escape($elem).'&';
                   1915:     }
                   1916:   }
                   1917:   $result=~s/\&$//;
1.265     albertel 1918:   $result .= '__END_ARRAY_REF__';
1.204     albertel 1919:   return $result;
                   1920: }
                   1921: 
1.168     albertel 1922: sub hash2str {
1.204     albertel 1923:   my (%hash) = @_;
                   1924:   my $result=&hashref2str(\%hash);
1.265     albertel 1925:   $result=~s/^__HASH_REF__//;
                   1926:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 1927:   return $result;
                   1928: }
                   1929: 
                   1930: sub hashref2str {
                   1931:   my ($hashref)=@_;
1.265     albertel 1932:   my $result='__HASH_REF__';
1.495     albertel 1933:   foreach (sort(keys(%$hashref))) {
1.204     albertel 1934:     if (ref($_) eq 'ARRAY') {
1.265     albertel 1935:       $result.=&arrayref2str($_).'=';
1.204     albertel 1936:     } elsif (ref($_) eq 'HASH') {
1.265     albertel 1937:       $result.=&hashref2str($_).'=';
1.204     albertel 1938:     } elsif (ref($_)) {
1.265     albertel 1939:       $result.='=';
                   1940:       #print("Got a ref of ".(ref($_))." skipping.");
1.204     albertel 1941:     } else {
1.265     albertel 1942: 	if ($_) {$result.=&escape($_).'=';} else { last; }
1.204     albertel 1943:     }
                   1944: 
1.265     albertel 1945:     if(ref($hashref->{$_}) eq 'ARRAY') {
                   1946:       $result.=&arrayref2str($hashref->{$_}).'&';
                   1947:     } elsif(ref($hashref->{$_}) eq 'HASH') {
                   1948:       $result.=&hashref2str($hashref->{$_}).'&';
                   1949:     } elsif(ref($hashref->{$_})) {
                   1950:        $result.='&';
                   1951:       #print("Got a ref of ".(ref($hashref->{$_}))." skipping.");
1.204     albertel 1952:     } else {
1.265     albertel 1953:       $result.=&escape($hashref->{$_}).'&';
1.204     albertel 1954:     }
                   1955:   }
1.168     albertel 1956:   $result=~s/\&$//;
1.265     albertel 1957:   $result .= '__END_HASH_REF__';
1.168     albertel 1958:   return $result;
                   1959: }
                   1960: 
                   1961: sub str2hash {
1.265     albertel 1962:     my ($string)=@_;
                   1963:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   1964:     return %$hash;
                   1965: }
                   1966: 
                   1967: sub str2hashref {
1.168     albertel 1968:   my ($string) = @_;
1.265     albertel 1969: 
                   1970:   my %hash;
                   1971: 
                   1972:   if($string !~ /^__HASH_REF__/) {
                   1973:       if (! ($string eq '' || !defined($string))) {
                   1974: 	  $hash{'error'}='Not hash reference';
                   1975:       }
                   1976:       return (\%hash, $string);
                   1977:   }
                   1978: 
                   1979:   $string =~ s/^__HASH_REF__//;
                   1980: 
                   1981:   while($string !~ /^__END_HASH_REF__/) {
                   1982:       #key
                   1983:       my $key='';
                   1984:       if($string =~ /^__HASH_REF__/) {
                   1985:           ($key, $string)=&str2hashref($string);
                   1986:           if(defined($key->{'error'})) {
                   1987:               $hash{'error'}='Bad data';
                   1988:               return (\%hash, $string);
                   1989:           }
                   1990:       } elsif($string =~ /^__ARRAY_REF__/) {
                   1991:           ($key, $string)=&str2arrayref($string);
                   1992:           if($key->[0] eq 'Array reference error') {
                   1993:               $hash{'error'}='Bad data';
                   1994:               return (\%hash, $string);
                   1995:           }
                   1996:       } else {
                   1997:           $string =~ s/^(.*?)=//;
1.267     albertel 1998: 	  $key=&unescape($1);
1.265     albertel 1999:       }
                   2000:       $string =~ s/^=//;
                   2001: 
                   2002:       #value
                   2003:       my $value='';
                   2004:       if($string =~ /^__HASH_REF__/) {
                   2005:           ($value, $string)=&str2hashref($string);
                   2006:           if(defined($value->{'error'})) {
                   2007:               $hash{'error'}='Bad data';
                   2008:               return (\%hash, $string);
                   2009:           }
                   2010:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2011:           ($value, $string)=&str2arrayref($string);
                   2012:           if($value->[0] eq 'Array reference error') {
                   2013:               $hash{'error'}='Bad data';
                   2014:               return (\%hash, $string);
                   2015:           }
                   2016:       } else {
                   2017: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   2018:       }
                   2019:       $string =~ s/^&//;
                   2020: 
                   2021:       $hash{$key}=$value;
1.204     albertel 2022:   }
1.265     albertel 2023: 
                   2024:   $string =~ s/^__END_HASH_REF__//;
                   2025: 
                   2026:   return (\%hash, $string);
1.204     albertel 2027: }
                   2028: 
                   2029: sub str2array {
1.265     albertel 2030:     my ($string)=@_;
                   2031:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   2032:     return @$array;
                   2033: }
                   2034: 
                   2035: sub str2arrayref {
1.204     albertel 2036:   my ($string) = @_;
1.265     albertel 2037:   my @array;
                   2038: 
                   2039:   if($string !~ /^__ARRAY_REF__/) {
                   2040:       if (! ($string eq '' || !defined($string))) {
                   2041: 	  $array[0]='Array reference error';
                   2042:       }
                   2043:       return (\@array, $string);
                   2044:   }
                   2045: 
                   2046:   $string =~ s/^__ARRAY_REF__//;
                   2047: 
                   2048:   while($string !~ /^__END_ARRAY_REF__/) {
                   2049:       my $value='';
                   2050:       if($string =~ /^__HASH_REF__/) {
                   2051:           ($value, $string)=&str2hashref($string);
                   2052:           if(defined($value->{'error'})) {
                   2053:               $array[0] ='Array reference error';
                   2054:               return (\@array, $string);
                   2055:           }
                   2056:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2057:           ($value, $string)=&str2arrayref($string);
                   2058:           if($value->[0] eq 'Array reference error') {
                   2059:               $array[0] ='Array reference error';
                   2060:               return (\@array, $string);
                   2061:           }
                   2062:       } else {
                   2063: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   2064:       }
                   2065:       $string =~ s/^&//;
                   2066: 
                   2067:       push(@array, $value);
1.191     harris41 2068:   }
1.265     albertel 2069: 
                   2070:   $string =~ s/^__END_ARRAY_REF__//;
                   2071: 
                   2072:   return (\@array, $string);
1.168     albertel 2073: }
                   2074: 
1.167     albertel 2075: # -------------------------------------------------------------------Temp Store
                   2076: 
1.168     albertel 2077: sub tmpreset {
                   2078:   my ($symb,$namespace,$domain,$stuname) = @_;
                   2079:   if (!$symb) {
                   2080:     $symb=&symbread();
1.380     albertel 2081:     if (!$symb) { $symb= $ENV{'request.url'}; }
1.168     albertel 2082:   }
                   2083:   $symb=escape($symb);
                   2084: 
                   2085:   if (!$namespace) { $namespace=$ENV{'request.state'}; }
                   2086:   $namespace=~s/\//\_/g;
                   2087:   $namespace=~s/\W//g;
                   2088: 
                   2089:   #FIXME needs to do something for /pub resources
                   2090:   if (!$domain) { $domain=$ENV{'user.domain'}; }
                   2091:   if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   2092:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2093:   my %hash;
                   2094:   if (tie(%hash,'GDBM_File',
                   2095: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2096: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 2097:     foreach my $key (keys %hash) {
1.180     albertel 2098:       if ($key=~ /:$symb/) {
1.168     albertel 2099: 	delete($hash{$key});
                   2100:       }
                   2101:     }
                   2102:   }
                   2103: }
                   2104: 
1.167     albertel 2105: sub tmpstore {
1.168     albertel 2106:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2107: 
                   2108:   if (!$symb) {
                   2109:     $symb=&symbread();
                   2110:     if (!$symb) { $symb= $ENV{'request.url'}; }
                   2111:   }
                   2112:   $symb=escape($symb);
                   2113: 
                   2114:   if (!$namespace) {
                   2115:     # I don't think we would ever want to store this for a course.
                   2116:     # it seems this will only be used if we don't have a course.
                   2117:     #$namespace=$ENV{'request.course.id'};
                   2118:     #if (!$namespace) {
                   2119:       $namespace=$ENV{'request.state'};
                   2120:     #}
                   2121:   }
                   2122:   $namespace=~s/\//\_/g;
                   2123:   $namespace=~s/\W//g;
                   2124: #FIXME needs to do something for /pub resources
                   2125:   if (!$domain) { $domain=$ENV{'user.domain'}; }
                   2126:   if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   2127:   my $now=time;
                   2128:   my %hash;
                   2129:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2130:   if (tie(%hash,'GDBM_File',
                   2131: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2132: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 2133:     $hash{"version:$symb"}++;
                   2134:     my $version=$hash{"version:$symb"};
                   2135:     my $allkeys=''; 
                   2136:     foreach my $key (keys(%$storehash)) {
                   2137:       $allkeys.=$key.':';
                   2138:       $hash{"$version:$symb:$key"}=$$storehash{$key};
                   2139:     }
                   2140:     $hash{"$version:$symb:timestamp"}=$now;
                   2141:     $allkeys.='timestamp';
                   2142:     $hash{"$version:keys:$symb"}=$allkeys;
                   2143:     if (untie(%hash)) {
                   2144:       return 'ok';
                   2145:     } else {
                   2146:       return "error:$!";
                   2147:     }
                   2148:   } else {
                   2149:     return "error:$!";
                   2150:   }
                   2151: }
1.167     albertel 2152: 
1.168     albertel 2153: # -----------------------------------------------------------------Temp Restore
1.167     albertel 2154: 
1.168     albertel 2155: sub tmprestore {
                   2156:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 2157: 
1.168     albertel 2158:   if (!$symb) {
                   2159:     $symb=&symbread();
                   2160:     if (!$symb) { $symb= $ENV{'request.url'}; }
                   2161:   }
                   2162:   $symb=escape($symb);
                   2163: 
                   2164:   if (!$namespace) { $namespace=$ENV{'request.state'}; }
                   2165:   #FIXME needs to do something for /pub resources
                   2166:   if (!$domain) { $domain=$ENV{'user.domain'}; }
                   2167:   if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   2168: 
                   2169:   my %returnhash;
                   2170:   $namespace=~s/\//\_/g;
                   2171:   $namespace=~s/\W//g;
                   2172:   my %hash;
                   2173:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2174:   if (tie(%hash,'GDBM_File',
                   2175: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2176: 	  &GDBM_READER(),0640)) {
1.168     albertel 2177:     my $version=$hash{"version:$symb"};
                   2178:     $returnhash{'version'}=$version;
                   2179:     my $scope;
                   2180:     for ($scope=1;$scope<=$version;$scope++) {
                   2181:       my $vkeys=$hash{"$scope:keys:$symb"};
                   2182:       my @keys=split(/:/,$vkeys);
                   2183:       my $key;
                   2184:       $returnhash{"$scope:keys"}=$vkeys;
                   2185:       foreach $key (@keys) {
                   2186: 	$returnhash{"$scope:$key"}=$hash{"$scope:$symb:$key"};
                   2187: 	$returnhash{"$key"}=$hash{"$scope:$symb:$key"};
1.167     albertel 2188:       }
                   2189:     }
1.168     albertel 2190:     if (!(untie(%hash))) {
                   2191:       return "error:$!";
                   2192:     }
                   2193:   } else {
                   2194:     return "error:$!";
                   2195:   }
                   2196:   return %returnhash;
1.167     albertel 2197: }
                   2198: 
1.9       www      2199: # ----------------------------------------------------------------------- Store
                   2200: 
                   2201: sub store {
1.124     www      2202:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2203:     my $home='';
                   2204: 
1.168     albertel 2205:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2206: 
1.213     www      2207:     $symb=&symbclean($symb);
1.122     albertel 2208:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      2209: 
1.325     www      2210:     if (!$domain) { $domain=$ENV{'user.domain'}; }
                   2211:     if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   2212: 
                   2213:     &devalidate($symb,$stuname,$domain);
1.109     www      2214: 
                   2215:     $symb=escape($symb);
1.187     www      2216:     if (!$namespace) { 
                   2217:        unless ($namespace=$ENV{'request.course.id'}) { 
                   2218:           return ''; 
                   2219:        } 
                   2220:     }
1.122     albertel 2221:     if (!$home) { $home=$ENV{'user.home'}; }
1.447     www      2222: 
                   2223:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   2224:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   2225: 
1.12      www      2226:     my $namevalue='';
1.191     harris41 2227:     foreach (keys %$storehash) {
1.122     albertel 2228:         $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
1.191     harris41 2229:     }
1.12      www      2230:     $namevalue=~s/\&$//;
1.187     www      2231:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124     www      2232:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9       www      2233: }
                   2234: 
1.47      www      2235: # -------------------------------------------------------------- Critical Store
                   2236: 
                   2237: sub cstore {
1.124     www      2238:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2239:     my $home='';
                   2240: 
1.168     albertel 2241:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2242: 
1.213     www      2243:     $symb=&symbclean($symb);
1.122     albertel 2244:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      2245: 
1.325     www      2246:     if (!$domain) { $domain=$ENV{'user.domain'}; }
                   2247:     if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   2248: 
                   2249:     &devalidate($symb,$stuname,$domain);
1.109     www      2250: 
                   2251:     $symb=escape($symb);
1.187     www      2252:     if (!$namespace) { 
                   2253:        unless ($namespace=$ENV{'request.course.id'}) { 
                   2254:           return ''; 
                   2255:        } 
                   2256:     }
1.122     albertel 2257:     if (!$home) { $home=$ENV{'user.home'}; }
1.447     www      2258: 
                   2259:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   2260:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 2261: 
1.47      www      2262:     my $namevalue='';
1.191     harris41 2263:     foreach (keys %$storehash) {
1.122     albertel 2264:         $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
1.191     harris41 2265:     }
1.47      www      2266:     $namevalue=~s/\&$//;
1.187     www      2267:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      2268:     return critical
                   2269:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47      www      2270: }
                   2271: 
1.9       www      2272: # --------------------------------------------------------------------- Restore
                   2273: 
                   2274: sub restore {
1.124     www      2275:     my ($symb,$namespace,$domain,$stuname) = @_;
                   2276:     my $home='';
                   2277: 
1.168     albertel 2278:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2279: 
1.122     albertel 2280:     if (!$symb) {
                   2281:       unless ($symb=escape(&symbread())) { return ''; }
                   2282:     } else {
1.213     www      2283:       $symb=&escape(&symbclean($symb));
1.122     albertel 2284:     }
1.188     www      2285:     if (!$namespace) { 
                   2286:        unless ($namespace=$ENV{'request.course.id'}) { 
                   2287:           return ''; 
                   2288:        } 
                   2289:     }
1.122     albertel 2290:     if (!$domain) { $domain=$ENV{'user.domain'}; }
                   2291:     if (!$stuname) { $stuname=$ENV{'user.name'}; }
                   2292:     if (!$home) { $home=$ENV{'user.home'}; }
                   2293:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   2294: 
1.12      www      2295:     my %returnhash=();
1.191     harris41 2296:     foreach (split(/\&/,$answer)) {
1.12      www      2297: 	my ($name,$value)=split(/\=/,$_);
                   2298:         $returnhash{&unescape($name)}=&unescape($value);
1.191     harris41 2299:     }
1.75      www      2300:     my $version;
                   2301:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.191     harris41 2302:        foreach (split(/\:/,$returnhash{$version.':keys'})) {
1.75      www      2303:           $returnhash{$_}=$returnhash{$version.':'.$_};
1.191     harris41 2304:        }
1.75      www      2305:     }
1.13      www      2306:     return %returnhash;
1.34      www      2307: }
                   2308: 
                   2309: # ---------------------------------------------------------- Course Description
                   2310: 
                   2311: sub coursedescription {
                   2312:     my $courseid=shift;
                   2313:     $courseid=~s/^\///;
1.49      www      2314:     $courseid=~s/\_/\//g;
1.34      www      2315:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 2316:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 2317:     my $normalid=$cdomain.'_'.$cnum;
                   2318:     # need to always cache even if we get errors otherwise we keep 
                   2319:     # trying and trying and trying to get the course description.
                   2320:     my %envhash=();
                   2321:     my %returnhash=();
                   2322:     $envhash{'course.'.$normalid.'.last_cache'}=time;
1.34      www      2323:     if ($chome ne 'no_host') {
1.302     albertel 2324:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 2325:        if (!exists($returnhash{'con_lost'})) {
                   2326:            $returnhash{'home'}= $chome;
                   2327: 	   $returnhash{'domain'} = $cdomain;
                   2328: 	   $returnhash{'num'} = $cnum;
1.130     albertel 2329:            while (my ($name,$value) = each %returnhash) {
1.53      www      2330:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 2331:            }
1.270     www      2332:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.34      www      2333:            $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.38      www      2334: 	       $ENV{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60      www      2335:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   2336:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   2337:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      2338:        }
                   2339:     }
1.302     albertel 2340:     &appenv(%envhash);
                   2341:     return %returnhash;
1.461     www      2342: }
                   2343: 
                   2344: # -------------------------------------------------See if a user is privileged
                   2345: 
                   2346: sub privileged {
                   2347:     my ($username,$domain)=@_;
                   2348:     my $rolesdump=&reply("dump:$domain:$username:roles",
                   2349: 			&homeserver($username,$domain));
                   2350:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
                   2351:     my $now=time;
                   2352:     if ($rolesdump ne '') {
                   2353:         foreach (split(/&/,$rolesdump)) {
                   2354: 	    if ($_!~/^rolesdef\&/) {
                   2355: 		my ($area,$role)=split(/=/,$_);
                   2356: 		$area=~s/\_\w\w$//;
                   2357: 		my ($trole,$tend,$tstart)=split(/_/,$role);
                   2358: 		if (($trole eq 'dc') || ($trole eq 'su')) {
                   2359: 		    my $active=1;
                   2360: 		    if ($tend) {
                   2361: 			if ($tend<$now) { $active=0; }
                   2362: 		    }
                   2363: 		    if ($tstart) {
                   2364: 			if ($tstart>$now) { $active=0; }
                   2365: 		    }
                   2366: 		    if ($active) { return 1; }
                   2367: 		}
                   2368: 	    }
                   2369: 	}
                   2370:     }
                   2371:     return 0;
1.9       www      2372: }
1.1       albertel 2373: 
1.103     harris41 2374: # -------------------------------------------------------- Get user privileges
1.11      www      2375: 
                   2376: sub rolesinit {
                   2377:     my ($domain,$username,$authhost)=@_;
                   2378:     my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.12      www      2379:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1.11      www      2380:     my %allroles=();
                   2381:     my %thesepriv=();
                   2382:     my $now=time;
1.21      www      2383:     my $userroles="user.login.time=$now\n";
1.11      www      2384:     my $thesestr;
                   2385: 
                   2386:     if ($rolesdump ne '') {
1.191     harris41 2387:         foreach (split(/&/,$rolesdump)) {
1.21      www      2388: 	  if ($_!~/^rolesdef\&/) {
1.11      www      2389:             my ($area,$role)=split(/=/,$_);
1.21      www      2390:             $area=~s/\_\w\w$//;
1.11      www      2391:             my ($trole,$tend,$tstart)=split(/_/,$role);
1.21      www      2392:             $userroles.='user.role.'.$trole.'.'.$area.'='.
                   2393:                         $tstart.'.'.$tend."\n";
1.349     www      2394: # log the associated role with the area
                   2395:             &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.11      www      2396:             if ($tend!=0) {
                   2397: 	        if ($tend<$now) {
                   2398: 	            $trole='';
                   2399:                 } 
                   2400:             }
                   2401:             if ($tstart!=0) {
                   2402:                 if ($tstart>$now) {
                   2403:                    $trole='';        
                   2404:                 }
                   2405:             }
                   2406:             if (($area ne '') && ($trole ne '')) {
1.347     albertel 2407: 		my $spec=$trole.'.'.$area;
                   2408: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   2409: 		if ($trole =~ /^cr\//) {
                   2410: 		    my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1.392     www      2411:  		    my $homsvr=homeserver($rauthor,$rdomain);
1.347     albertel 2412: 		    if ($hostname{$homsvr} ne '') {
1.392     www      2413: 			my ($rdummy,$roledef)=
                   2414: 			   &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   2415: 				
                   2416: 			if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.347     albertel 2417: 			    my ($syspriv,$dompriv,$coursepriv)=
1.392     www      2418: 				split(/\_/,$roledef);
1.347     albertel 2419: 			    if (defined($syspriv)) {
                   2420: 				$allroles{'cm./'}.=':'.$syspriv;
                   2421: 				$allroles{$spec.'./'}.=':'.$syspriv;
                   2422: 			    }
                   2423: 			    if ($tdomain ne '') {
                   2424: 				if (defined($dompriv)) {
                   2425: 				    $allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   2426: 				    $allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   2427: 				}
                   2428: 				if ($trest ne '') {
                   2429: 				    if (defined($coursepriv)) {
                   2430: 					$allroles{'cm.'.$area}.=':'.$coursepriv;
                   2431: 					$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   2432: 				    }
                   2433: 				}
                   2434: 			    }
                   2435: 			}
                   2436: 		    }
                   2437: 		} else {
                   2438: 		    if (defined($pr{$trole.':s'})) {
                   2439: 			$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   2440: 			$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   2441: 		    }
                   2442: 		    if ($tdomain ne '') {
                   2443: 			if (defined($pr{$trole.':d'})) {
                   2444: 			    $allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   2445: 			    $allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   2446: 			}
                   2447: 			if ($trest ne '') {
                   2448: 			    if (defined($pr{$trole.':c'})) {
                   2449: 				$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   2450: 				$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   2451: 			    }
                   2452: 			}
                   2453: 		    }
                   2454: 		}
1.12      www      2455:             }
                   2456:           } 
1.191     harris41 2457:         }
1.125     www      2458:         my $adv=0;
1.128     www      2459:         my $author=0;
1.191     harris41 2460:         foreach (keys %allroles) {
1.11      www      2461:             %thesepriv=();
1.146     www      2462:             if (($_!~/^st/) && ($_!~/^ta/) && ($_!~/^cm/)) { $adv=1; }
1.128     www      2463:             if (($_=~/^au/) || ($_=~/^ca/)) { $author=1; }
1.191     harris41 2464:             foreach (split(/:/,$allroles{$_})) {
1.11      www      2465:                 if ($_ ne '') {
1.103     harris41 2466: 		    my ($privilege,$restrictions)=split(/&/,$_);
1.11      www      2467:                     if ($restrictions eq '') {
1.103     harris41 2468: 			$thesepriv{$privilege}='F';
1.11      www      2469:                     } else {
1.103     harris41 2470:                         if ($thesepriv{$privilege} ne 'F') {
                   2471: 			    $thesepriv{$privilege}.=$restrictions;
1.11      www      2472:                         }
                   2473:                     }
                   2474:                 }
1.191     harris41 2475:             }
1.11      www      2476:             $thesestr='';
1.191     harris41 2477:             foreach (keys %thesepriv) { $thesestr.=':'.$_.'&'.$thesepriv{$_}; }
1.11      www      2478:             $userroles.='user.priv.'.$_.'='.$thesestr."\n";
1.191     harris41 2479:         }
1.128     www      2480:         $userroles.='user.adv='.$adv."\n".
                   2481: 	            'user.author='.$author."\n";
1.126     www      2482:         $ENV{'user.adv'}=$adv;
1.11      www      2483:     }
                   2484:     return $userroles;  
                   2485: }
                   2486: 
1.12      www      2487: # --------------------------------------------------------------- get interface
                   2488: 
                   2489: sub get {
1.131     albertel 2490:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      2491:    my $items='';
1.191     harris41 2492:    foreach (@$storearr) {
1.12      www      2493:        $items.=escape($_).'&';
1.191     harris41 2494:    }
1.12      www      2495:    $items=~s/\&$//;
1.131     albertel 2496:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   2497:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   2498:    my $uhome=&homeserver($uname,$udomain);
                   2499: 
1.133     albertel 2500:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      2501:    my @pairs=split(/\&/,$rep);
1.273     albertel 2502:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   2503:      return @pairs;
                   2504:    }
1.15      www      2505:    my %returnhash=();
1.42      www      2506:    my $i=0;
1.191     harris41 2507:    foreach (@$storearr) {
1.42      www      2508:       $returnhash{$_}=unescape($pairs[$i]);
                   2509:       $i++;
1.191     harris41 2510:    }
1.15      www      2511:    return %returnhash;
1.27      www      2512: }
                   2513: 
                   2514: # --------------------------------------------------------------- del interface
                   2515: 
                   2516: sub del {
1.133     albertel 2517:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      2518:    my $items='';
1.191     harris41 2519:    foreach (@$storearr) {
1.27      www      2520:        $items.=escape($_).'&';
1.191     harris41 2521:    }
1.27      www      2522:    $items=~s/\&$//;
1.133     albertel 2523:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   2524:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   2525:    my $uhome=&homeserver($uname,$udomain);
                   2526: 
                   2527:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      2528: }
                   2529: 
                   2530: # -------------------------------------------------------------- dump interface
                   2531: 
                   2532: sub dump {
1.193     www      2533:    my ($namespace,$udomain,$uname,$regexp)=@_;
1.129     albertel 2534:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   2535:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   2536:    my $uhome=&homeserver($uname,$udomain);
1.193     www      2537:    if ($regexp) {
                   2538:        $regexp=&escape($regexp);
                   2539:    } else {
                   2540:        $regexp='.';
                   2541:    }
                   2542:    my $rep=reply("dump:$udomain:$uname:$namespace:$regexp",$uhome);
1.12      www      2543:    my @pairs=split(/\&/,$rep);
                   2544:    my %returnhash=();
1.191     harris41 2545:    foreach (@pairs) {
1.12      www      2546:       my ($key,$value)=split(/=/,$_);
1.29      www      2547:       $returnhash{unescape($key)}=unescape($value);
1.318     matthew  2548:    }
                   2549:    return %returnhash;
1.407     www      2550: }
                   2551: 
                   2552: # -------------------------------------------------------------- keys interface
                   2553: 
                   2554: sub getkeys {
                   2555:    my ($namespace,$udomain,$uname)=@_;
                   2556:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   2557:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   2558:    my $uhome=&homeserver($uname,$udomain);
                   2559:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   2560:    my @keyarray=();
                   2561:    foreach (split(/\&/,$rep)) {
                   2562:       push (@keyarray,&unescape($_));
                   2563:    }
                   2564:    return @keyarray;
1.318     matthew  2565: }
                   2566: 
1.319     matthew  2567: # --------------------------------------------------------------- currentdump
                   2568: sub currentdump {
1.328     matthew  2569:    my ($courseid,$sdom,$sname)=@_;
1.326     matthew  2570:    $courseid = $ENV{'request.course.id'} if (! defined($courseid));
                   2571:    $sdom     = $ENV{'user.domain'}       if (! defined($sdom));
                   2572:    $sname    = $ENV{'user.name'}         if (! defined($sname));
                   2573:    my $uhome = &homeserver($sname,$sdom);
                   2574:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318     matthew  2575:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  2576:    #
1.318     matthew  2577:    my %returnhash=();
1.319     matthew  2578:    #
                   2579:    if ($rep eq "unknown_cmd") { 
                   2580:        # an old lond will not know currentdump
                   2581:        # Do a dump and make it look like a currentdump
1.326     matthew  2582:        my @tmp = &dump($courseid,$sdom,$sname,'.');
1.319     matthew  2583:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   2584:        my %hash = @tmp;
                   2585:        @tmp=();
1.424     matthew  2586:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  2587:    } else {
                   2588:        my @pairs=split(/\&/,$rep);
                   2589:        foreach (@pairs) {
                   2590:            my ($key,$value)=split(/=/,$_);
                   2591:            my ($symb,$param) = split(/:/,$key);
                   2592:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
                   2593:                                                           &unescape($value);
                   2594:        }
1.191     harris41 2595:    }
1.12      www      2596:    return %returnhash;
1.424     matthew  2597: }
                   2598: 
                   2599: sub convert_dump_to_currentdump{
                   2600:     my %hash = %{shift()};
                   2601:     my %returnhash;
                   2602:     # Code ripped from lond, essentially.  The only difference
                   2603:     # here is the unescaping done by lonnet::dump().  Conceivably
                   2604:     # we might run in to problems with parameter names =~ /^v\./
                   2605:     while (my ($key,$value) = each(%hash)) {
                   2606:         my ($v,$symb,$param) = split(/:/,$key);
                   2607:         next if ($v eq 'version' || $symb eq 'keys');
                   2608:         next if (exists($returnhash{$symb}) &&
                   2609:                  exists($returnhash{$symb}->{$param}) &&
                   2610:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   2611:         $returnhash{$symb}->{$param}=$value;
                   2612:         $returnhash{$symb}->{'v.'.$param}=$v;
                   2613:     }
                   2614:     #
                   2615:     # Remove all of the keys in the hashes which keep track of
                   2616:     # the version of the parameter.
                   2617:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   2618:         # use a foreach because we are going to delete from the hash.
                   2619:         foreach my $key (keys(%$param_hash)) {
                   2620:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   2621:         }
                   2622:     }
                   2623:     return \%returnhash;
1.12      www      2624: }
                   2625: 
1.449     matthew  2626: # --------------------------------------------------------------- inc interface
                   2627: 
                   2628: sub inc {
                   2629:     my ($namespace,$store,$udomain,$uname) = @_;
                   2630:     if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   2631:     if (!$uname) { $uname=$ENV{'user.name'}; }
                   2632:     my $uhome=&homeserver($uname,$udomain);
                   2633:     my $items='';
                   2634:     if (! ref($store)) {
                   2635:         # got a single value, so use that instead
                   2636:         $items = &escape($store).'=&';
                   2637:     } elsif (ref($store) eq 'SCALAR') {
                   2638:         $items = &escape($$store).'=&';        
                   2639:     } elsif (ref($store) eq 'ARRAY') {
                   2640:         $items = join('=&',map {&escape($_);} @{$store});
                   2641:     } elsif (ref($store) eq 'HASH') {
                   2642:         while (my($key,$value) = each(%{$store})) {
                   2643:             $items.= &escape($key).'='.&escape($value).'&';
                   2644:         }
                   2645:     }
                   2646:     $items=~s/\&$//;
                   2647:     return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   2648: }
                   2649: 
1.12      www      2650: # --------------------------------------------------------------- put interface
                   2651: 
                   2652: sub put {
1.134     albertel 2653:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   2654:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   2655:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   2656:    my $uhome=&homeserver($uname,$udomain);
1.12      www      2657:    my $items='';
1.191     harris41 2658:    foreach (keys %$storehash) {
1.134     albertel 2659:        $items.=&escape($_).'='.&escape($$storehash{$_}).'&';
1.191     harris41 2660:    }
1.12      www      2661:    $items=~s/\&$//;
1.134     albertel 2662:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      2663: }
                   2664: 
1.524     raeburn  2665: # ---------------------------------------------------------- putstore interface
                   2666:                                                                                      
                   2667: sub putstore {
                   2668:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   2669:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   2670:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   2671:    my $uhome=&homeserver($uname,$udomain);
                   2672:    my $items='';
                   2673:    my %allitems = ();
                   2674:    foreach (keys %$storehash) {
                   2675:        if ($_ =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
                   2676:            my $key = $1.':keys:'.$2;
                   2677:            $allitems{$key} .= $3.':';
                   2678:        }
                   2679:        $items.=$_.'='.&escape($$storehash{$_}).'&';
                   2680:    }
                   2681:    foreach (keys %allitems) {
                   2682:        $allitems{$_} =~ s/\:$//;
                   2683:        $items.= $_.'='.$allitems{$_}.'&';
                   2684:    }
                   2685:    $items=~s/\&$//;
                   2686:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
                   2687: }
                   2688: 
1.47      www      2689: # ------------------------------------------------------ critical put interface
                   2690: 
                   2691: sub cput {
1.134     albertel 2692:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   2693:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   2694:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   2695:    my $uhome=&homeserver($uname,$udomain);
1.47      www      2696:    my $items='';
1.191     harris41 2697:    foreach (keys %$storehash) {
1.134     albertel 2698:        $items.=escape($_).'='.escape($$storehash{$_}).'&';
1.191     harris41 2699:    }
1.47      www      2700:    $items=~s/\&$//;
1.134     albertel 2701:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      2702: }
                   2703: 
                   2704: # -------------------------------------------------------------- eget interface
                   2705: 
                   2706: sub eget {
1.133     albertel 2707:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      2708:    my $items='';
1.191     harris41 2709:    foreach (@$storearr) {
1.12      www      2710:        $items.=escape($_).'&';
1.191     harris41 2711:    }
1.12      www      2712:    $items=~s/\&$//;
1.133     albertel 2713:    if (!$udomain) { $udomain=$ENV{'user.domain'}; }
                   2714:    if (!$uname) { $uname=$ENV{'user.name'}; }
                   2715:    my $uhome=&homeserver($uname,$udomain);
                   2716:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      2717:    my @pairs=split(/\&/,$rep);
                   2718:    my %returnhash=();
1.42      www      2719:    my $i=0;
1.191     harris41 2720:    foreach (@$storearr) {
1.42      www      2721:       $returnhash{$_}=unescape($pairs[$i]);
                   2722:       $i++;
1.191     harris41 2723:    }
1.12      www      2724:    return %returnhash;
                   2725: }
                   2726: 
1.341     www      2727: # ---------------------------------------------- Custom access rule evaluation
                   2728: 
                   2729: sub customaccess {
                   2730:     my ($priv,$uri)=@_;
1.342     www      2731:     my ($urole,$urealm)=split(/\./,$ENV{'request.role'});
1.343     www      2732:     $urealm=~s/^\W//;
                   2733:     my ($udom,$ucrs,$usec)=split(/\//,$urealm);
1.341     www      2734:     my $access=0;
                   2735:     foreach (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.342     www      2736: 	my ($effect,$realm,$role)=split(/\:/,$_);
1.343     www      2737:         if ($role) {
                   2738: 	   if ($role ne $urole) { next; }
                   2739:         }
                   2740:         foreach (split(/\s*\,\s*/,$realm)) {
                   2741:             my ($tdom,$tcrs,$tsec)=split(/\_/,$_);
                   2742:             if ($tdom) {
                   2743: 		if ($tdom ne $udom) { next; }
                   2744:             }
                   2745:             if ($tcrs) {
                   2746: 		if ($tcrs ne $ucrs) { next; }
                   2747:             }
                   2748:             if ($tsec) {
                   2749: 		if ($tsec ne $usec) { next; }
                   2750:             }
                   2751:             $access=($effect eq 'allow');
                   2752:             last;
1.342     www      2753:         }
1.402     bowersj2 2754: 	if ($realm eq '' && $role eq '') {
                   2755:             $access=($effect eq 'allow');
                   2756: 	}
1.341     www      2757:     }
                   2758:     return $access;
                   2759: }
                   2760: 
1.103     harris41 2761: # ------------------------------------------------- Check for a user privilege
1.12      www      2762: 
                   2763: sub allowed {
                   2764:     my ($priv,$uri)=@_;
1.439     www      2765:     $uri=&deversion($uri);
1.152     www      2766:     my $orguri=$uri;
1.52      www      2767:     $uri=&declutter($uri);
1.29      www      2768: 
1.398     albertel 2769:     if (defined($ENV{'allowed.'.$priv})) { return $ENV{'allowed.'.$priv}; }
1.54      www      2770: # Free bre access to adm and meta resources
1.529     albertel 2771:     if (((($uri=~/^adm\//) && ($uri !~ m|/bulletinboard$|)) 
                   2772: 	 || ($uri=~/\.meta$/)) && ($priv eq 'bre')) {
1.14      www      2773: 	return 'F';
1.159     www      2774:     }
                   2775: 
                   2776: # Free bre to public access
                   2777: 
                   2778:     if ($priv eq 'bre') {
1.238     www      2779:         my $copyright=&metadata($uri,'copyright');
1.301     www      2780: 	if (($copyright eq 'public') && (!$ENV{'request.course.id'})) { 
                   2781:            return 'F'; 
                   2782:         }
1.238     www      2783:         if ($copyright eq 'priv') {
                   2784:             $uri=~/([^\/]+)\/([^\/]+)\//;
                   2785: 	    unless (($ENV{'user.name'} eq $2) && ($ENV{'user.domain'} eq $1)) {
                   2786: 		return '';
                   2787:             }
                   2788:         }
                   2789:         if ($copyright eq 'domain') {
                   2790:             $uri=~/([^\/]+)\/([^\/]+)\//;
                   2791: 	    unless (($ENV{'user.domain'} eq $1) ||
                   2792:                  ($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $1)) {
                   2793: 		return '';
                   2794:             }
1.262     matthew  2795:         }
                   2796:         if ($ENV{'request.role'}=~ /li\.\//) {
                   2797:             # Library role, so allow browsing of resources in this domain.
                   2798:             return 'F';
1.238     www      2799:         }
1.341     www      2800:         if ($copyright eq 'custom') {
                   2801: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   2802:         }
1.14      www      2803:     }
1.264     matthew  2804:     # Domain coordinator is trying to create a course
                   2805:     if (($priv eq 'ccc') && ($ENV{'request.role'} =~ /^dc\./)) {
                   2806:         # uri is the requested domain in this case.
                   2807:         # comparison to 'request.role.domain' shows if the user has selected
                   2808:         # a role of dc for the domain in question. 
                   2809:         return 'F' if ($uri eq $ENV{'request.role.domain'});
                   2810:     }
1.29      www      2811: 
1.52      www      2812:     my $thisallowed='';
                   2813:     my $statecond=0;
                   2814:     my $courseprivid='';
                   2815: 
                   2816: # Course
                   2817: 
1.479     albertel 2818:     if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.52      www      2819:        $thisallowed.=$1;
                   2820:     }
1.29      www      2821: 
1.52      www      2822: # Domain
                   2823: 
                   2824:     if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 2825:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      2826:        $thisallowed.=$1;
                   2827:     }
1.52      www      2828: 
                   2829: # Course: uri itself is a course
1.66      www      2830:     my $courseuri=$uri;
                   2831:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      2832:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      2833: 
1.83      www      2834:     if ($ENV{'user.priv.'.$ENV{'request.role'}.'.'.$courseuri}
1.479     albertel 2835:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      2836:        $thisallowed.=$1;
                   2837:     }
1.29      www      2838: 
1.314     www      2839: # URI is an uploaded document for this course
                   2840: 
1.492     albertel 2841:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
                   2842: 	my $refuri=$ENV{'httpref.'.$orguri};
                   2843: 	if ($refuri) {
                   2844: 	    if ($refuri =~ m|^/adm/|) {
                   2845: 		$thisallowed='F';
                   2846: 	    }
                   2847: 	}
1.314     www      2848:     }
1.492     albertel 2849: 
1.52      www      2850: # Full access at system, domain or course-wide level? Exit.
1.29      www      2851: 
                   2852:     if ($thisallowed=~/F/) {
                   2853: 	return 'F';
                   2854:     }
                   2855: 
1.52      www      2856: # If this is generating or modifying users, exit with special codes
1.29      www      2857: 
1.479     albertel 2858:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:'=~/\:\Q$priv\E\:/) {
1.52      www      2859: 	return $thisallowed;
                   2860:     }
                   2861: #
1.103     harris41 2862: # Gathered so far: system, domain and course wide privileges
1.52      www      2863: #
                   2864: # Course: See if uri or referer is an individual resource that is part of 
                   2865: # the course
                   2866: 
                   2867:     if ($ENV{'request.course.id'}) {
1.232     www      2868: 
1.52      www      2869:        $courseprivid=$ENV{'request.course.id'};
                   2870:        if ($ENV{'request.course.sec'}) {
                   2871:           $courseprivid.='/'.$ENV{'request.course.sec'};
                   2872:        }
                   2873:        $courseprivid=~s/\_/\//;
                   2874:        my $checkreferer=1;
1.232     www      2875:        my ($match,$cond)=&is_on_map($uri);
                   2876:        if ($match) {
                   2877:            $statecond=$cond;
1.52      www      2878:            if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
1.479     albertel 2879:                =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      2880:                $thisallowed.=$1;
                   2881:                $checkreferer=0;
                   2882:            }
1.29      www      2883:        }
1.83      www      2884:        
1.148     www      2885:        if ($checkreferer) {
1.152     www      2886: 	  my $refuri=$ENV{'httpref.'.$orguri};
1.148     www      2887:             unless ($refuri) {
1.191     harris41 2888:                 foreach (keys %ENV) {
1.148     www      2889: 		    if ($_=~/^httpref\..*\*/) {
                   2890: 			my $pattern=$_;
1.156     www      2891:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      2892:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   2893:                         $pattern=~s/\//\\\//g;
1.152     www      2894:                         if ($orguri=~/$pattern/) {
1.148     www      2895: 			    $refuri=$ENV{$_};
                   2896:                         }
                   2897:                     }
1.191     harris41 2898:                 }
1.148     www      2899:             }
1.232     www      2900: 
1.148     www      2901:          if ($refuri) { 
1.152     www      2902: 	  $refuri=&declutter($refuri);
1.232     www      2903:           my ($match,$cond)=&is_on_map($refuri);
                   2904:             if ($match) {
                   2905:               my $refstatecond=$cond;
1.52      www      2906:               if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
1.479     albertel 2907:                   =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      2908:                   $thisallowed.=$1;
1.53      www      2909:                   $uri=$refuri;
                   2910:                   $statecond=$refstatecond;
1.52      www      2911:               }
                   2912:           }
1.148     www      2913:         }
1.29      www      2914:        }
1.52      www      2915:    }
1.29      www      2916: 
1.52      www      2917: #
1.103     harris41 2918: # Gathered now: all privileges that could apply, and condition number
1.52      www      2919: # 
                   2920: #
                   2921: # Full or no access?
                   2922: #
1.29      www      2923: 
1.52      www      2924:     if ($thisallowed=~/F/) {
                   2925: 	return 'F';
                   2926:     }
1.29      www      2927: 
1.52      www      2928:     unless ($thisallowed) {
                   2929:         return '';
                   2930:     }
1.29      www      2931: 
1.52      www      2932: # Restrictions exist, deal with them
                   2933: #
                   2934: #   C:according to course preferences
                   2935: #   R:according to resource settings
                   2936: #   L:unless locked
                   2937: #   X:according to user session state
                   2938: #
                   2939: 
                   2940: # Possibly locked functionality, check all courses
1.54      www      2941: # Locks might take effect only after 10 minutes cache expiration for other
                   2942: # courses, and 2 minutes for current course
1.52      www      2943: 
                   2944:     my $envkey;
                   2945:     if ($thisallowed=~/L/) {
                   2946:         foreach $envkey (keys %ENV) {
1.54      www      2947:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   2948:                my $courseid=$2;
                   2949:                my $roleid=$1.'.'.$2;
1.92      www      2950:                $courseid=~s/^\///;
1.54      www      2951:                my $expiretime=600;
                   2952:                if ($ENV{'request.role'} eq $roleid) {
                   2953: 		  $expiretime=120;
                   2954:                }
                   2955: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   2956:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
                   2957:                if ((time-$ENV{$prefix.'last_cache'})>$expiretime) {
                   2958: 		   &coursedescription($courseid);
                   2959:                }
1.479     albertel 2960:                if (($ENV{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
1.54      www      2961:                 || ($ENV{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   2962: 		   if ($ENV{$prefix.'res.'.$uri.'.lock.expire'}>time) {
1.57      www      2963:                        &log($ENV{'user.domain'},$ENV{'user.name'},
1.239     www      2964:                             $ENV{'user.home'},
1.57      www      2965:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      2966:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.54      www      2967:                             $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      2968: 		       return '';
                   2969:                    }
                   2970:                }
1.479     albertel 2971:                if (($ENV{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
1.54      www      2972:                 || ($ENV{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   2973: 		   if ($ENV{'priv.'.$priv.'.lock.expire'}>time) {
1.57      www      2974:                        &log($ENV{'user.domain'},$ENV{'user.name'},
1.239     www      2975:                             $ENV{'user.home'},
1.57      www      2976:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      2977:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.54      www      2978:                             $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      2979: 		       return '';
                   2980:                    }
                   2981:                }
                   2982: 	   }
1.29      www      2983:        }
1.52      www      2984:     }
                   2985:    
                   2986: #
                   2987: # Rest of the restrictions depend on selected course
                   2988: #
                   2989: 
                   2990:     unless ($ENV{'request.course.id'}) {
                   2991:        return '1';
                   2992:     }
1.29      www      2993: 
1.52      www      2994: #
                   2995: # Now user is definitely in a course
                   2996: #
1.53      www      2997: 
                   2998: 
                   2999: # Course preferences
                   3000: 
                   3001:    if ($thisallowed=~/C/) {
1.54      www      3002:        my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
1.237     www      3003:        my $unamedom=$ENV{'user.name'}.':'.$ENV{'user.domain'};
1.54      www      3004:        if ($ENV{'course.'.$ENV{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 3005: 	   =~/\Q$rolecode\E/) {
1.57      www      3006:            &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
                   3007:                 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
1.237     www      3008:                 $ENV{'request.course.id'});
                   3009:            return '';
                   3010:        }
                   3011: 
                   3012:        if ($ENV{'course.'.$ENV{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 3013: 	   =~/\Q$unamedom\E/) {
1.237     www      3014:            &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
                   3015:                 'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
1.54      www      3016:                 $ENV{'request.course.id'});
                   3017:            return '';
                   3018:        }
1.53      www      3019:    }
                   3020: 
                   3021: # Resource preferences
                   3022: 
                   3023:    if ($thisallowed=~/R/) {
1.54      www      3024:        my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
1.479     albertel 3025:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.341     www      3026: 	  &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
1.57      www      3027:                     'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
1.341     www      3028:           return '';
1.54      www      3029:        }
1.53      www      3030:    }
1.30      www      3031: 
1.246     www      3032: # Restricted by state or randomout?
1.30      www      3033: 
1.52      www      3034:    if ($thisallowed=~/X/) {
1.247     www      3035:       if ($ENV{'acc.randomout'}) {
1.249     www      3036:          my $symb=&symbread($uri,1);
1.479     albertel 3037:          if (($symb) && ($ENV{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      3038:             return ''; 
                   3039:          }
1.247     www      3040:       }
                   3041:       if (&condval($statecond)) {
1.52      www      3042: 	 return '2';
                   3043:       } else {
                   3044:          return '';
                   3045:       }
                   3046:    }
1.30      www      3047: 
1.52      www      3048:    return 'F';
1.232     www      3049: }
                   3050: 
                   3051: # --------------------------------------------------- Is a resource on the map?
                   3052: 
                   3053: sub is_on_map {
                   3054:     my $uri=&declutter(shift);
1.435     www      3055:     $uri=~s/\.\d+\.(\w+)$/\.$1/;
1.232     www      3056:     my @uriparts=split(/\//,$uri);
                   3057:     my $filename=$uriparts[$#uriparts];
                   3058:     my $pathname=$uri;
1.289     bowersj2 3059:     $pathname=~s|/\Q$filename\E$||;
1.332     www      3060:     $pathname=~s/^adm\/wrapper\///;    
1.289     bowersj2 3061:     #Trying to find the conditional for the file
1.232     www      3062:     my $match=($ENV{'acc.res.'.$ENV{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 3063: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      3064:     if ($match) {
1.289     bowersj2 3065: 	return (1,$1);
                   3066:     } else {
1.434     www      3067: 	return (0,0);
1.289     bowersj2 3068:     }
1.12      www      3069: }
                   3070: 
1.427     www      3071: # --------------------------------------------------------- Get symb from alias
                   3072: 
                   3073: sub get_symb_from_alias {
                   3074:     my $symb=shift;
                   3075:     my ($map,$resid,$url)=&decode_symb($symb);
                   3076: # Already is a symb
                   3077:     if ($url) { return $symb; }
                   3078: # Must be an alias
                   3079:     my $aliassymb='';
                   3080:     my %bighash;
                   3081:     if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
                   3082:                             &GDBM_READER(),0640)) {
                   3083:         my $rid=$bighash{'mapalias_'.$symb};
                   3084: 	if ($rid) {
                   3085: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 3086: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   3087: 				    $resid,$bighash{'src_'.$rid});
1.427     www      3088: 	}
                   3089:         untie %bighash;
                   3090:     }
                   3091:     return $aliassymb;
                   3092: }
                   3093: 
1.12      www      3094: # ----------------------------------------------------------------- Define Role
                   3095: 
                   3096: sub definerole {
                   3097:   if (allowed('mcr','/')) {
                   3098:     my ($rolename,$sysrole,$domrole,$courole)=@_;
1.392     www      3099:     foreach (split(':',$sysrole)) {
1.21      www      3100: 	my ($crole,$cqual)=split(/\&/,$_);
1.479     albertel 3101:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   3102:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   3103: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      3104:                return "refused:s:$crole&$cqual"; 
                   3105:             }
                   3106:         }
1.191     harris41 3107:     }
1.392     www      3108:     foreach (split(':',$domrole)) {
1.21      www      3109: 	my ($crole,$cqual)=split(/\&/,$_);
1.479     albertel 3110:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   3111:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   3112: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      3113:                return "refused:d:$crole&$cqual"; 
                   3114:             }
                   3115:         }
1.191     harris41 3116:     }
1.392     www      3117:     foreach (split(':',$courole)) {
1.21      www      3118: 	my ($crole,$cqual)=split(/\&/,$_);
1.479     albertel 3119:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   3120:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   3121: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      3122:                return "refused:c:$crole&$cqual"; 
                   3123:             }
                   3124:         }
1.191     harris41 3125:     }
1.12      www      3126:     my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
                   3127:                 "$ENV{'user.domain'}:$ENV{'user.name'}:".
1.21      www      3128: 	        "rolesdef_$rolename=".
                   3129:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.12      www      3130:     return reply($command,$ENV{'user.home'});
                   3131:   } else {
                   3132:     return 'refused';
                   3133:   }
1.105     harris41 3134: }
                   3135: 
                   3136: # ---------------- Make a metadata query against the network of library servers
                   3137: 
                   3138: sub metadata_query {
1.244     matthew  3139:     my ($query,$custom,$customshow,$server_array)=@_;
1.120     harris41 3140:     my %rhash;
1.244     matthew  3141:     my @server_list = (defined($server_array) ? @$server_array
                   3142:                                               : keys(%libserv) );
                   3143:     for my $server (@server_list) {
1.118     harris41 3144: 	unless ($custom or $customshow) {
                   3145: 	    my $reply=&reply("querysend:".&escape($query),$server);
                   3146: 	    $rhash{$server}=$reply;
                   3147: 	}
                   3148: 	else {
                   3149: 	    my $reply=&reply("querysend:".&escape($query).':'.
                   3150: 			     &escape($custom).':'.&escape($customshow),
                   3151: 			     $server);
                   3152: 	    $rhash{$server}=$reply;
                   3153: 	}
1.112     harris41 3154:     }
1.118     harris41 3155:     return \%rhash;
1.240     www      3156: }
                   3157: 
                   3158: # ----------------------------------------- Send log queries and wait for reply
                   3159: 
                   3160: sub log_query {
                   3161:     my ($uname,$udom,$query,%filters)=@_;
                   3162:     my $uhome=&homeserver($uname,$udom);
                   3163:     if ($uhome eq 'no_host') { return 'error: no_host'; }
                   3164:     my $uhost=$hostname{$uhome};
1.241     www      3165:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys %filters));
1.240     www      3166:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   3167:                        $uhome);
1.479     albertel 3168:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      3169:     return get_query_reply($queryid);
                   3170: }
                   3171: 
1.508     raeburn  3172: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  3173: 
                   3174: sub fetch_enrollment_query {
1.511     raeburn  3175:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508     raeburn  3176:     my $homeserver;
                   3177:     if ($context eq 'automated') {
                   3178:         $homeserver = $perlvar{'lonHostID'};
                   3179:     } else {
                   3180:         $homeserver = &homeserver($cnum,$dom);
                   3181:     }
1.506     raeburn  3182:     my $host=$hostname{$homeserver};
                   3183:     my $cmd = '';
                   3184:     foreach (keys %{$affiliatesref}) {
1.508     raeburn  3185:         $cmd .= $_.'='.join(",",@{$$affiliatesref{$_}}).'%%';
1.506     raeburn  3186:     }
                   3187:     $cmd =~ s/%%$//;
                   3188:     $cmd = &escape($cmd);
                   3189:     my $query = 'fetchenrollment';
                   3190:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$ENV{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  3191:     unless ($queryid=~/^\Q$host\E\_/) { 
                   3192:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   3193:         return 'error: '.$queryid;
                   3194:     }
1.506     raeburn  3195:     my $reply = &get_query_reply($queryid);
1.526     raeburn  3196:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   3197:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$ENV{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum);
                   3198:     } else {
1.515     raeburn  3199:         my @responses = split/:/,$reply;
                   3200:         if ($homeserver eq $perlvar{'lonHostID'}) {
                   3201:             foreach (@responses) {
                   3202:                 my ($key,$value) = split/=/,$_;
                   3203:                 $$replyref{$key} = $value;
                   3204:             }
                   3205:         } else {
1.506     raeburn  3206:             my $pathname = $perlvar{'lonDaemons'}.'/tmp';
                   3207:             foreach (@responses) {
                   3208:                 my ($key,$value) = split/=/,$_;
                   3209:                 $$replyref{$key} = $value;
                   3210:                 if ($value > 0) {
                   3211:                     foreach (@{$$affiliatesref{$key}}) {
                   3212:                         my $filename = $dom.'_'.$key.'_'.$_.'_classlist.xml';
                   3213:                         my $destname = $pathname.'/'.$filename;
                   3214:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  3215:                         if ($xml_classlist =~ /^error/) {
                   3216:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   3217:                         } else {
1.506     raeburn  3218:                             if ( open(FILE,">$destname") ) {
                   3219:                                 print FILE &unescape($xml_classlist);
                   3220:                                 close(FILE);
1.526     raeburn  3221:                             } else {
                   3222:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  3223:                             }
                   3224:                         }
                   3225:                     }
                   3226:                 }
                   3227:             }
                   3228:         }
                   3229:         return 'ok';
                   3230:     }
                   3231:     return 'error';
                   3232: }
                   3233: 
1.242     www      3234: sub get_query_reply {
                   3235:     my $queryid=shift;
1.240     www      3236:     my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
                   3237:     my $reply='';
                   3238:     for (1..100) {
                   3239: 	sleep 2;
                   3240:         if (-e $replyfile.'.end') {
1.448     albertel 3241: 	    if (open(my $fh,$replyfile)) {
1.240     www      3242:                $reply.=<$fh>;
1.448     albertel 3243:                close($fh);
1.240     www      3244: 	   } else { return 'error: reply_file_error'; }
1.242     www      3245:            return &unescape($reply);
                   3246: 	}
1.240     www      3247:     }
1.242     www      3248:     return 'timeout:'.$queryid;
1.240     www      3249: }
                   3250: 
                   3251: sub courselog_query {
1.241     www      3252: #
                   3253: # possible filters:
                   3254: # url: url or symb
                   3255: # username
                   3256: # domain
                   3257: # action: view, submit, grade
                   3258: # start: timestamp
                   3259: # end: timestamp
                   3260: #
1.240     www      3261:     my (%filters)=@_;
                   3262:     unless ($ENV{'request.course.id'}) { return 'no_course'; }
1.241     www      3263:     if ($filters{'url'}) {
                   3264: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   3265:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   3266:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   3267:     }
1.240     www      3268:     my $cname=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                   3269:     my $cdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                   3270:     return &log_query($cname,$cdom,'courselog',%filters);
                   3271: }
                   3272: 
                   3273: sub userlog_query {
                   3274:     my ($uname,$udom,%filters)=@_;
                   3275:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      3276: }
                   3277: 
1.506     raeburn  3278: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   3279: 
                   3280: sub auto_run {
1.508     raeburn  3281:     my ($cnum,$cdom) = @_;
                   3282:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  3283:     my $response = &reply('autorun:'.$cdom,$homeserver);
1.506     raeburn  3284:     return $response;
                   3285: }
                   3286:                                                                                    
                   3287: sub auto_get_sections {
1.508     raeburn  3288:     my ($cnum,$cdom,$inst_coursecode) = @_;
                   3289:     my $homeserver = &homeserver($cnum,$cdom);
1.506     raeburn  3290:     my @secs = ();
1.511     raeburn  3291:     my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
1.506     raeburn  3292:     unless ($response eq 'refused') {
                   3293:         @secs = split/:/,$response;
                   3294:     }
                   3295:     return @secs;
                   3296: }
                   3297:                                                                                    
                   3298: sub auto_new_course {
1.508     raeburn  3299:     my ($cnum,$cdom,$inst_course_id,$owner) = @_;
                   3300:     my $homeserver = &homeserver($cnum,$cdom);
1.515     raeburn  3301:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
1.506     raeburn  3302:     return $response;
                   3303: }
                   3304:                                                                                    
                   3305: sub auto_validate_courseID {
1.508     raeburn  3306:     my ($cnum,$cdom,$inst_course_id) = @_;
                   3307:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  3308:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  3309:     return $response;
                   3310: }
                   3311:                                                                                    
                   3312: sub auto_create_password {
1.508     raeburn  3313:     my ($cnum,$cdom,$authparam) = @_;
                   3314:     my $homeserver = &homeserver($cnum,$cdom); 
1.506     raeburn  3315:     my $create_passwd = 0;
                   3316:     my $authchk = '';
1.511     raeburn  3317:     my $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
1.506     raeburn  3318:     if ($response eq 'refused') {
                   3319:         $authchk = 'refused';
                   3320:     } else {
                   3321:         ($authparam,$create_passwd,$authchk) = split/:/,$response;
                   3322:     }
                   3323:     return ($authparam,$create_passwd,$authchk);
                   3324: }
                   3325: 
1.521     raeburn  3326: sub auto_instcode_format {
                   3327:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,$cat_order) = @_;
                   3328:     my $courses = '';
                   3329:     my $homeserver;
                   3330:     if ($caller eq 'global') {
                   3331:         $homeserver = $perlvar{'lonHostID'};
                   3332:     } else {
                   3333:         $homeserver = &homeserver($caller,$codedom);
                   3334:     }
                   3335:     my $host=$hostname{$homeserver};
                   3336:     foreach (keys %{$instcodes}) {
                   3337:         $courses .= &escape($_).'='.&escape($$instcodes{$_}).'&';
                   3338:     }
                   3339:     chop($courses);
                   3340:     my $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$homeserver);
                   3341:     unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   3342:         my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = split/:/,$response;
                   3343:         %{$codes} = &str2hash($codes_str);
                   3344:         @{$codetitles} = &str2array($codetitles_str);
                   3345:         %{$cat_titles} = &str2hash($cat_titles_str);
                   3346:         %{$cat_order} = &str2hash($cat_order_str);
                   3347:         return 'ok';
                   3348:     }
                   3349:     return $response;
                   3350: }
                   3351: 
1.12      www      3352: # ------------------------------------------------------------------ Plain Text
                   3353: 
                   3354: sub plaintext {
1.22      www      3355:     my $short=shift;
1.414     www      3356:     return &mt($prp{$short});
1.12      www      3357: }
                   3358: 
                   3359: # ----------------------------------------------------------------- Assign Role
                   3360: 
                   3361: sub assignrole {
1.357     www      3362:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
1.21      www      3363:     my $mrole;
                   3364:     if ($role =~ /^cr\//) {
1.393     www      3365:         my $cwosec=$url;
                   3366:         $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
                   3367: 	unless (&allowed('ccr',$cwosec)) {
1.104     www      3368:            &logthis('Refused custom assignrole: '.
                   3369:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   3370: 		    $ENV{'user.name'}.' at '.$ENV{'user.domain'});
                   3371:            return 'refused'; 
                   3372:         }
1.21      www      3373:         $mrole='cr';
                   3374:     } else {
1.82      www      3375:         my $cwosec=$url;
1.83      www      3376:         $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
1.373     www      3377:         unless ((&allowed('c'.$role,$cwosec)) || &allowed('c'.$role,$udom)) { 
1.104     www      3378:            &logthis('Refused assignrole: '.
                   3379:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   3380: 		    $ENV{'user.name'}.' at '.$ENV{'user.domain'});
                   3381:            return 'refused'; 
                   3382:         }
1.21      www      3383:         $mrole=$role;
                   3384:     }
                   3385:     my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
                   3386:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      3387:     if ($end) { $command.='_'.$end; }
1.21      www      3388:     if ($start) {
                   3389: 	if ($end) { 
1.81      www      3390:            $command.='_'.$start; 
1.21      www      3391:         } else {
1.81      www      3392:            $command.='_0_'.$start;
1.21      www      3393:         }
                   3394:     }
1.357     www      3395: # actually delete
                   3396:     if ($deleteflag) {
1.373     www      3397: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      3398: # modify command to delete the role
                   3399:            $command="encrypt:rolesdel:$ENV{'user.domain'}:$ENV{'user.name'}:".
                   3400:                 "$udom:$uname:$url".'_'."$mrole";
1.373     www      3401: 	   &logthis("$ENV{'user.name'} at $ENV{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      3402: # set start and finish to negative values for userrolelog
                   3403:            $start=-1;
                   3404:            $end=-1;
                   3405:         }
                   3406:     }
                   3407: # send command
1.349     www      3408:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      3409: # log new user role if status is ok
1.349     www      3410:     if ($answer eq 'ok') {
                   3411: 	&userrolelog($mrole,$uname,$udom,$url,$start,$end);
                   3412:     }
                   3413:     return $answer;
1.169     harris41 3414: }
                   3415: 
                   3416: # -------------------------------------------------- Modify user authentication
1.197     www      3417: # Overrides without validation
                   3418: 
1.169     harris41 3419: sub modifyuserauth {
                   3420:     my ($udom,$uname,$umode,$upass)=@_;
                   3421:     my $uhome=&homeserver($uname,$udom);
1.197     www      3422:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   3423:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.272     matthew  3424:              $umode.' by '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                   3425:              ' in domain '.$ENV{'request.role.domain'});  
1.169     harris41 3426:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   3427: 		     &escape($upass),$uhome);
1.197     www      3428:     &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.home'},
                   3429:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   3430:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   3431:     &log($udom,,$uname,$uhome,
                   3432:         'Authentication changed by '.$ENV{'user.domain'}.', '.
                   3433:                                      $ENV{'user.name'}.', '.$umode.
                   3434:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 3435:     unless ($reply eq 'ok') {
1.197     www      3436:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 3437: 	return 'error: '.$reply;
                   3438:     }   
1.170     harris41 3439:     return 'ok';
1.80      www      3440: }
                   3441: 
1.81      www      3442: # --------------------------------------------------------------- Modify a user
1.80      www      3443: 
1.81      www      3444: sub modifyuser {
1.206     matthew  3445:     my ($udom,    $uname, $uid,
                   3446:         $umode,   $upass, $first,
                   3447:         $middle,  $last,  $gene,
1.387     www      3448:         $forceid, $desiredhome, $email)=@_;
1.198     www      3449:     $udom=~s/\W//g;
                   3450:     $uname=~s/\W//g;
1.81      www      3451:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      3452:              $umode.', '.$first.', '.$middle.', '.
1.206     matthew  3453: 	     $last.', '.$gene.'(forceid: '.$forceid.')'.
                   3454:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   3455:                                      ' desiredhome not specified'). 
1.272     matthew  3456:              ' by '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                   3457:              ' in domain '.$ENV{'request.role.domain'});
1.230     stredwic 3458:     my $uhome=&homeserver($uname,$udom,'true');
1.80      www      3459: # ----------------------------------------------------------------- Create User
1.406     albertel 3460:     if (($uhome eq 'no_host') && 
                   3461: 	(($umode && $upass) || ($umode eq 'localauth'))) {
1.80      www      3462:         my $unhome='';
1.209     matthew  3463:         if (defined($desiredhome) && $hostdom{$desiredhome} eq $udom) { 
                   3464:             $unhome = $desiredhome;
                   3465: 	} elsif($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $udom) {
1.80      www      3466: 	    $unhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1.209     matthew  3467:         } else { # load balancing routine for determining $unhome
1.80      www      3468:             my $tryserver;
1.81      www      3469:             my $loadm=10000000;
1.80      www      3470:             foreach $tryserver (keys %libserv) {
                   3471: 	       if ($hostdom{$tryserver} eq $udom) {
                   3472:                   my $answer=reply('load',$tryserver);
                   3473:                   if (($answer=~/\d+/) && ($answer<$loadm)) {
                   3474: 		      $loadm=$answer;
                   3475:                       $unhome=$tryserver;
                   3476:                   }
                   3477: 	       }
                   3478: 	    }
                   3479:         }
                   3480:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  3481: 	    return 'error: unable to find a home server for '.$uname.
                   3482:                    ' in domain '.$udom;
1.80      www      3483:         }
                   3484:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   3485:                          &escape($upass),$unhome);
                   3486: 	unless ($reply eq 'ok') {
                   3487:             return 'error: '.$reply;
                   3488:         }   
1.230     stredwic 3489:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      3490:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  3491: 	    return 'error: unable verify users home machine.';
1.80      www      3492:         }
1.209     matthew  3493:     }   # End of creation of new user
1.80      www      3494: # ---------------------------------------------------------------------- Add ID
                   3495:     if ($uid) {
                   3496:        $uid=~tr/A-Z/a-z/;
                   3497:        my %uidhash=&idrget($udom,$uname);
1.196     www      3498:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   3499:          && (!$forceid)) {
1.80      www      3500: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  3501: 	      return 'error: user id "'.$uid.'" does not match '.
                   3502:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      3503:           }
                   3504:        } else {
                   3505: 	  &idput($udom,($uname => $uid));
                   3506:        }
                   3507:     }
                   3508: # -------------------------------------------------------------- Add names, etc
1.313     matthew  3509:     my @tmp=&get('environment',
1.134     albertel 3510: 		   ['firstname','middlename','lastname','generation'],
                   3511: 		   $udom,$uname);
1.313     matthew  3512:     my %names;
                   3513:     if ($tmp[0] =~ m/^error:.*/) { 
                   3514:         %names=(); 
                   3515:     } else {
                   3516:         %names = @tmp;
                   3517:     }
1.388     www      3518: #
                   3519: # Make sure to not trash student environment if instructor does not bother
                   3520: # to supply name and email information
                   3521: #
                   3522:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  3523:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      3524:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  3525:     if (defined($gene))   { $names{'generation'} = $gene; }
1.388     www      3526:     if ($email)  { $names{'notification'} = $email;
                   3527:                    $names{'critnotification'} = $email; }
1.387     www      3528: 
1.134     albertel 3529:     my $reply = &put('environment', \%names, $udom,$uname);
                   3530:     if ($reply ne 'ok') { return 'error: '.$reply; }
1.81      www      3531:     &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      3532:              $umode.', '.$first.', '.$middle.', '.
                   3533: 	     $last.', '.$gene.' by '.
                   3534:              $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1.134     albertel 3535:     return 'ok';
1.80      www      3536: }
                   3537: 
1.81      www      3538: # -------------------------------------------------------------- Modify student
1.80      www      3539: 
1.81      www      3540: sub modifystudent {
                   3541:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.515     raeburn  3542:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
1.455     albertel 3543:     if (!$cid) {
                   3544: 	unless ($cid=$ENV{'request.course.id'}) {
                   3545: 	    return 'not_in_class';
                   3546: 	}
1.80      www      3547:     }
                   3548: # --------------------------------------------------------------- Make the user
1.81      www      3549:     my $reply=&modifyuser
1.209     matthew  3550: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.387     www      3551:          $desiredhome,$email);
1.80      www      3552:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  3553:     # This will cause &modify_student_enrollment to get the uid from the
                   3554:     # students environment
                   3555:     $uid = undef if (!$forceid);
1.455     albertel 3556:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.515     raeburn  3557: 					$gene,$usec,$end,$start,$type,$locktype,$cid);
1.297     matthew  3558:     return $reply;
                   3559: }
                   3560: 
                   3561: sub modify_student_enrollment {
1.515     raeburn  3562:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
1.455     albertel 3563:     my ($cdom,$cnum,$chome);
                   3564:     if (!$cid) {
                   3565: 	unless ($cid=$ENV{'request.course.id'}) {
                   3566: 	    return 'not_in_class';
                   3567: 	}
                   3568: 	$cdom=$ENV{'course.'.$cid.'.domain'};
                   3569: 	$cnum=$ENV{'course.'.$cid.'.num'};
                   3570:     } else {
                   3571: 	($cdom,$cnum)=split(/_/,$cid);
                   3572:     }
                   3573:     $chome=$ENV{'course.'.$cid.'.home'};
                   3574:     if (!$chome) {
1.457     raeburn  3575: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  3576:     }
1.455     albertel 3577:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  3578:     # Make sure the user exists
1.81      www      3579:     my $uhome=&homeserver($uname,$udom);
                   3580:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   3581: 	return 'error: no such user';
                   3582:     }
1.297     matthew  3583:     # Get student data if we were not given enough information
                   3584:     if (!defined($first)  || $first  eq '' || 
                   3585:         !defined($last)   || $last   eq '' || 
                   3586:         !defined($uid)    || $uid    eq '' || 
                   3587:         !defined($middle) || $middle eq '' || 
                   3588:         !defined($gene)   || $gene   eq '') {
1.294     matthew  3589:         # They did not supply us with enough data to enroll the student, so
                   3590:         # we need to pick up more information.
1.297     matthew  3591:         my %tmp = &get('environment',
1.294     matthew  3592:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  3593:                        ,$udom,$uname);
                   3594: 
1.455     albertel 3595:         #foreach (keys(%tmp)) {
                   3596:         #    &logthis("key $_ = ".$tmp{$_});
                   3597:         #}
1.294     matthew  3598:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   3599:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   3600:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  3601:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  3602:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   3603:     }
                   3604:     my $fullname = &Apache::loncoursedata::ProcessFullName($last,$gene,
                   3605:                                                            $first,$middle);
1.487     albertel 3606:     my $reply=cput('classlist',
                   3607: 		   {"$uname:$udom" => 
1.515     raeburn  3608: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487     albertel 3609: 		   $cdom,$cnum);
1.81      www      3610:     unless (($reply eq 'ok') || ($reply eq 'delayed')) {
                   3611: 	return 'error: '.$reply;
                   3612:     }
1.297     matthew  3613:     # Add student role to user
1.83      www      3614:     my $uurl='/'.$cid;
1.81      www      3615:     $uurl=~s/\_/\//g;
                   3616:     if ($usec) {
                   3617: 	$uurl.='/'.$usec;
                   3618:     }
                   3619:     return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1.21      www      3620: }
                   3621: 
1.84      www      3622: # ------------------------------------------------- Write to course preferences
                   3623: 
                   3624: sub writecoursepref {
                   3625:     my ($courseid,%prefs)=@_;
                   3626:     $courseid=~s/^\///;
                   3627:     $courseid=~s/\_/\//g;
                   3628:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   3629:     my $chome=homeserver($cnum,$cdomain);
                   3630:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   3631: 	return 'error: no such course';
                   3632:     }
                   3633:     my $cstring='';
1.191     harris41 3634:     foreach (keys %prefs) {
1.84      www      3635: 	$cstring.=escape($_).'='.escape($prefs{$_}).'&';
1.191     harris41 3636:     }
1.84      www      3637:     $cstring=~s/\&$//;
                   3638:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   3639: }
                   3640: 
                   3641: # ---------------------------------------------------------- Make/modify course
                   3642: 
                   3643: sub createcourse {
1.516     raeburn  3644:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code)=@_;
1.84      www      3645:     $url=&declutter($url);
                   3646:     my $cid='';
1.264     matthew  3647:     unless (&allowed('ccc',$udom)) {
1.84      www      3648:         return 'refused';
                   3649:     }
                   3650: # ------------------------------------------------------------------- Create ID
                   3651:    my $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   3652:        unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   3653: # ----------------------------------------------- Make sure that does not exist
1.230     stredwic 3654:    my $uhome=&homeserver($uname,$udom,'true');
1.84      www      3655:    unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   3656:        $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   3657:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1.230     stredwic 3658:        $uhome=&homeserver($uname,$udom,'true');       
1.84      www      3659:        unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   3660:            return 'error: unable to generate unique course-ID';
                   3661:        } 
                   3662:    }
1.264     matthew  3663: # ------------------------------------------------ Check supplied server name
                   3664:     $course_server = $ENV{'user.homeserver'} if (! defined($course_server));
                   3665:     if (! exists($libserv{$course_server})) {
                   3666:         return 'error:bad server name '.$course_server;
                   3667:     }
1.84      www      3668: # ------------------------------------------------------------- Make the course
                   3669:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  3670:                       $course_server);
1.84      www      3671:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.230     stredwic 3672:     $uhome=&homeserver($uname,$udom,'true');
1.84      www      3673:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   3674: 	return 'error: no such course';
                   3675:     }
1.271     www      3676: # ----------------------------------------------------------------- Course made
1.516     raeburn  3677: # log existence
                   3678:     &courseidput($udom,&escape($udom.'_'.$uname).'='.&escape($description).
                   3679:                  '='.&escape($inst_code),$uhome);
1.358     www      3680:     &flushcourselogs();
                   3681: # set toplevel url
1.271     www      3682:     my $topurl=$url;
                   3683:     unless ($nonstandard) {
                   3684: # ------------------------------------------ For standard courses, make top url
                   3685:         my $mapurl=&clutter($url);
1.278     www      3686:         if ($mapurl eq '/res/') { $mapurl=''; }
1.271     www      3687:         $ENV{'form.initmap'}=(<<ENDINITMAP);
                   3688: <map>
                   3689: <resource id="1" type="start"></resource>
                   3690: <resource id="2" src="$mapurl"></resource>
                   3691: <resource id="3" type="finish"></resource>
                   3692: <link index="1" from="1" to="2"></link>
                   3693: <link index="2" from="2" to="3"></link>
                   3694: </map>
                   3695: ENDINITMAP
                   3696:         $topurl=&declutter(
                   3697:         &finishuserfileupload($uname,$udom,$uhome,'initmap','default.sequence')
                   3698:                           );
                   3699:     }
                   3700: # ----------------------------------------------------------- Write preferences
1.84      www      3701:     &writecoursepref($udom.'_'.$uname,
                   3702:                      ('description' => $description,
1.271     www      3703:                       'url'         => $topurl));
1.84      www      3704:     return '/'.$udom.'/'.$uname;
                   3705: }
                   3706: 
1.21      www      3707: # ---------------------------------------------------------- Assign Custom Role
                   3708: 
                   3709: sub assigncustomrole {
1.357     www      3710:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
1.21      www      3711:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.357     www      3712:                        $end,$start,$deleteflag);
1.21      www      3713: }
                   3714: 
                   3715: # ----------------------------------------------------------------- Revoke Role
                   3716: 
                   3717: sub revokerole {
1.357     www      3718:     my ($udom,$uname,$url,$role,$deleteflag)=@_;
1.21      www      3719:     my $now=time;
1.357     www      3720:     return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
1.21      www      3721: }
                   3722: 
                   3723: # ---------------------------------------------------------- Revoke Custom Role
                   3724: 
                   3725: sub revokecustomrole {
1.357     www      3726:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
1.21      www      3727:     my $now=time;
1.357     www      3728:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
                   3729:            $deleteflag);
1.17      www      3730: }
                   3731: 
1.533     banghart 3732: # ------------------------------------------------------------ Disk usage
1.535     albertel 3733: sub diskusage {
1.533     banghart 3734:     my ($udom,$uname,$directoryRoot)=@_;
                   3735:     $directoryRoot =~ s/\/$//;
1.535     albertel 3736:     my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
1.514     albertel 3737:     return $listing;
1.512     banghart 3738: }
                   3739: 
                   3740: 
1.17      www      3741: # ------------------------------------------------------------ Directory lister
                   3742: 
                   3743: sub dirlist {
1.253     stredwic 3744:     my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
                   3745: 
1.18      www      3746:     $uri=~s/^\///;
                   3747:     $uri=~s/\/$//;
1.253     stredwic 3748:     my ($udom, $uname);
                   3749:     (undef,$udom,$uname)=split(/\//,$uri);
                   3750:     if(defined($userdomain)) {
                   3751:         $udom = $userdomain;
                   3752:     }
                   3753:     if(defined($username)) {
                   3754:         $uname = $username;
                   3755:     }
                   3756: 
                   3757:     my $dirRoot = $perlvar{'lonDocRoot'};
                   3758:     if(defined($alternateDirectoryRoot)) {
                   3759:         $dirRoot = $alternateDirectoryRoot;
                   3760:         $dirRoot =~ s/\/$//;
                   3761:     }
                   3762: 
                   3763:     if($udom) {
                   3764:         if($uname) {
                   3765:             my $listing=reply('ls:'.$dirRoot.'/'.$uri,
                   3766:                               homeserver($uname,$udom));
                   3767:             return split(/:/,$listing);
                   3768:         } elsif(!defined($alternateDirectoryRoot)) {
                   3769:             my $tryserver;
                   3770:             my %allusers=();
                   3771:             foreach $tryserver (keys %libserv) {
                   3772:                 if($hostdom{$tryserver} eq $udom) {
                   3773:                     my $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   3774:                                       $udom, $tryserver);
                   3775:                     if (($listing ne 'no_such_dir') && ($listing ne 'empty')
                   3776:                         && ($listing ne 'con_lost')) {
                   3777:                         foreach (split(/:/,$listing)) {
                   3778:                             my ($entry,@stat)=split(/&/,$_);
                   3779:                             $allusers{$entry}=1;
                   3780:                         }
                   3781:                     }
1.191     harris41 3782:                 }
1.253     stredwic 3783:             }
                   3784:             my $alluserstr='';
                   3785:             foreach (sort keys %allusers) {
                   3786:                 $alluserstr.=$_.'&user:';
                   3787:             }
                   3788:             $alluserstr=~s/:$//;
                   3789:             return split(/:/,$alluserstr);
                   3790:         } else {
                   3791:             my @emptyResults = ();
                   3792:             push(@emptyResults, 'missing user name');
                   3793:             return split(':',@emptyResults);
                   3794:         }
                   3795:     } elsif(!defined($alternateDirectoryRoot)) {
                   3796:         my $tryserver;
                   3797:         my %alldom=();
                   3798:         foreach $tryserver (keys %libserv) {
                   3799:             $alldom{$hostdom{$tryserver}}=1;
                   3800:         }
                   3801:         my $alldomstr='';
                   3802:         foreach (sort keys %alldom) {
1.397     albertel 3803:             $alldomstr.=$perlvar{'lonDocRoot'}.'/res/'.$_.'/&domain:';
1.253     stredwic 3804:         }
                   3805:         $alldomstr=~s/:$//;
                   3806:         return split(/:/,$alldomstr);       
                   3807:     } else {
                   3808:         my @emptyResults = ();
                   3809:         push(@emptyResults, 'missing domain');
                   3810:         return split(':',@emptyResults);
1.275     stredwic 3811:     }
                   3812: }
                   3813: 
                   3814: # --------------------------------------------- GetFileTimestamp
                   3815: # This function utilizes dirlist and returns the date stamp for
                   3816: # when it was last modified.  It will also return an error of -1
                   3817: # if an error occurs
                   3818: 
1.410     matthew  3819: ##
                   3820: ## FIXME: This subroutine assumes its caller knows something about the
                   3821: ## directory structure of the home server for the student ($root).
                   3822: ## Not a good assumption to make.  Since this is for looking up files
                   3823: ## in user directories, the full path should be constructed by lond, not
                   3824: ## whatever machine we request data from.
                   3825: ##
1.275     stredwic 3826: sub GetFileTimestamp {
                   3827:     my ($studentDomain,$studentName,$filename,$root)=@_;
                   3828:     $studentDomain=~s/\W//g;
                   3829:     $studentName=~s/\W//g;
                   3830:     my $subdir=$studentName.'__';
                   3831:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   3832:     my $proname="$studentDomain/$subdir/$studentName";
                   3833:     $proname .= '/'.$filename;
1.375     matthew  3834:     my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain, 
                   3835:                                               $studentName, $root);
1.275     stredwic 3836:     my @stats = split('&', $fileStat);
                   3837:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375     matthew  3838:         # @stats contains first the filename, then the stat output
                   3839:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 3840:     } else {
                   3841:         return -1;
1.253     stredwic 3842:     }
1.26      www      3843: }
                   3844: 
                   3845: # -------------------------------------------------------- Value of a Condition
                   3846: 
1.40      www      3847: sub directcondval {
                   3848:     my $number=shift;
                   3849:     if ($ENV{'user.state.'.$ENV{'request.course.id'}}) {
                   3850:        return substr($ENV{'user.state.'.$ENV{'request.course.id'}},$number,1);
                   3851:     } else {
                   3852:        return 2;
                   3853:     }
                   3854: }
                   3855: 
1.26      www      3856: sub condval {
                   3857:     my $condidx=shift;
                   3858:     my $result=0;
1.54      www      3859:     my $allpathcond='';
1.191     harris41 3860:     foreach (split(/\|/,$condidx)) {
1.54      www      3861:        if (defined($ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_})) {
                   3862: 	   $allpathcond.=
                   3863:                '('.$ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_}.')|';
                   3864:        }
1.191     harris41 3865:     }
1.54      www      3866:     $allpathcond=~s/\|$//;
1.33      www      3867:     if ($ENV{'request.course.id'}) {
1.54      www      3868:        if ($allpathcond) {
1.26      www      3869:           my $operand='|';
                   3870: 	  my @stack;
1.191     harris41 3871:            foreach ($allpathcond=~/(\d+|\(|\)|\&|\|)/g) {
1.26      www      3872:               if ($_ eq '(') {
                   3873:                  push @stack,($operand,$result)
                   3874:               } elsif ($_ eq ')') {
                   3875:                   my $before=pop @stack;
                   3876: 		  if (pop @stack eq '&') {
                   3877: 		      $result=$result>$before?$before:$result;
                   3878:                   } else {
                   3879:                       $result=$result>$before?$result:$before;
                   3880:                   }
                   3881:               } elsif (($_ eq '&') || ($_ eq '|')) {
                   3882:                   $operand=$_;
                   3883:               } else {
1.40      www      3884:                   my $new=directcondval($_);
1.26      www      3885:                   if ($operand eq '&') {
                   3886:                      $result=$result>$new?$new:$result;
                   3887:                   } else {
                   3888:                      $result=$result>$new?$result:$new;
1.191     harris41 3889:                   }
1.26      www      3890:               }
1.191     harris41 3891:           }
1.26      www      3892:        }
                   3893:     }
                   3894:     return $result;
1.421     albertel 3895: }
                   3896: 
                   3897: # ---------------------------------------------------- Devalidate courseresdata
                   3898: 
                   3899: sub devalidatecourseresdata {
                   3900:     my ($coursenum,$coursedomain)=@_;
                   3901:     my $hashid=$coursenum.':'.$coursedomain;
1.428     albertel 3902:     &devalidate_cache(\%courseresdatacache,$hashid,'courseres');
1.28      www      3903: }
                   3904: 
1.200     www      3905: # --------------------------------------------------- Course Resourcedata Query
                   3906: 
                   3907: sub courseresdata {
                   3908:     my ($coursenum,$coursedomain,@which)=@_;
                   3909:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   3910:     my $hashid=$coursenum.':'.$coursedomain;
1.425     albertel 3911:     my ($result,$cached)=&is_cached(\%courseresdatacache,$hashid,'courseres');
1.417     albertel 3912:     unless (defined($cached)) {
1.251     albertel 3913: 	my %dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 3914: 	$result=\%dumpreply;
1.251     albertel 3915: 	my ($tmp) = keys(%dumpreply);
                   3916: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.425     albertel 3917: 	    &do_cache(\%courseresdatacache,$hashid,$result,'courseres');
1.306     albertel 3918: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   3919: 	    return $tmp;
1.416     albertel 3920: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 3921: 	    $result=undef;
1.425     albertel 3922: 	    &do_cache(\%courseresdatacache,$hashid,$result,'courseres');
1.250     albertel 3923: 	}
                   3924:     }
1.251     albertel 3925:     foreach my $item (@which) {
1.417     albertel 3926: 	if (defined($result->{$item})) {
                   3927: 	    return $result->{$item};
1.251     albertel 3928: 	}
1.250     albertel 3929:     }
1.291     albertel 3930:     return undef;
1.200     www      3931: }
                   3932: 
1.379     matthew  3933: #
                   3934: # EXT resource caching routines
                   3935: #
                   3936: 
                   3937: sub clear_EXT_cache_status {
1.383     albertel 3938:     &delenv('cache.EXT.');
1.379     matthew  3939: }
                   3940: 
                   3941: sub EXT_cache_status {
                   3942:     my ($target_domain,$target_user) = @_;
1.383     albertel 3943:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.389     www      3944:     if (exists($ENV{$cachename}) && ($ENV{$cachename}+600) > time) {
1.379     matthew  3945:         # We know already the user has no data
                   3946:         return 1;
                   3947:     } else {
                   3948:         return 0;
                   3949:     }
                   3950: }
                   3951: 
                   3952: sub EXT_cache_set {
                   3953:     my ($target_domain,$target_user) = @_;
1.383     albertel 3954:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.379     matthew  3955:     &appenv($cachename => time);
                   3956: }
                   3957: 
1.28      www      3958: # --------------------------------------------------------- Value of a Variable
1.58      www      3959: sub EXT {
1.395     albertel 3960:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.218     albertel 3961: 
1.68      www      3962:     unless ($varname) { return ''; }
1.218     albertel 3963:     #get real user name/domain, courseid and symb
                   3964:     my $courseid;
1.359     albertel 3965:     my $publicuser;
1.427     www      3966:     if ($symbparm) {
                   3967: 	$symbparm=&get_symb_from_alias($symbparm);
                   3968:     }
1.218     albertel 3969:     if (!($uname && $udom)) {
1.360     albertel 3970:       (my $cursymb,$courseid,$udom,$uname,$publicuser)=
1.378     matthew  3971: 	  &Apache::lonxml::whichuser($symbparm);
1.218     albertel 3972:       if (!$symbparm) {	$symbparm=$cursymb; }
                   3973:     } else {
                   3974: 	$courseid=$ENV{'request.course.id'};
                   3975:     }
1.48      www      3976:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   3977:     my $rest;
1.320     albertel 3978:     if (defined($therest[0])) {
1.48      www      3979:        $rest=join('.',@therest);
                   3980:     } else {
                   3981:        $rest='';
                   3982:     }
1.320     albertel 3983: 
1.57      www      3984:     my $qualifierrest=$qualifier;
                   3985:     if ($rest) { $qualifierrest.='.'.$rest; }
                   3986:     my $spacequalifierrest=$space;
                   3987:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      3988:     if ($realm eq 'user') {
1.48      www      3989: # --------------------------------------------------------------- user.resource
                   3990: 	if ($space eq 'resource') {
1.335     albertel 3991: 	    if (defined($Apache::lonhomework::parsing_a_problem)) {
                   3992: 		return $Apache::lonhomework::history{$qualifierrest};
                   3993: 	    } else {
1.359     albertel 3994: 		my %restored;
                   3995: 		if ($publicuser || $ENV{'request.state'} eq 'construct') {
                   3996: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   3997: 		} else {
                   3998: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   3999: 		}
1.335     albertel 4000: 		return $restored{$qualifierrest};
                   4001: 	    }
1.48      www      4002: # ----------------------------------------------------------------- user.access
                   4003:         } elsif ($space eq 'access') {
1.218     albertel 4004: 	    # FIXME - not supporting calls for a specific user
1.48      www      4005:             return &allowed($qualifier,$rest);
                   4006: # ------------------------------------------ user.preferences, user.environment
                   4007:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.218     albertel 4008: 	    if (($uname eq $ENV{'user.name'}) &&
                   4009: 		($udom eq $ENV{'user.domain'})) {
                   4010: 		return $ENV{join('.',('environment',$qualifierrest))};
                   4011: 	    } else {
1.359     albertel 4012: 		my %returnhash;
                   4013: 		if (!$publicuser) {
                   4014: 		    %returnhash=&userenvironment($udom,$uname,
                   4015: 						 $qualifierrest);
                   4016: 		}
1.218     albertel 4017: 		return $returnhash{$qualifierrest};
                   4018: 	    }
1.48      www      4019: # ----------------------------------------------------------------- user.course
                   4020:         } elsif ($space eq 'course') {
1.218     albertel 4021: 	    # FIXME - not supporting calls for a specific user
1.48      www      4022:             return $ENV{join('.',('request.course',$qualifier))};
                   4023: # ------------------------------------------------------------------- user.role
                   4024:         } elsif ($space eq 'role') {
1.218     albertel 4025: 	    # FIXME - not supporting calls for a specific user
1.48      www      4026:             my ($role,$where)=split(/\./,$ENV{'request.role'});
                   4027:             if ($qualifier eq 'value') {
                   4028: 		return $role;
                   4029:             } elsif ($qualifier eq 'extent') {
                   4030:                 return $where;
                   4031:             }
                   4032: # ----------------------------------------------------------------- user.domain
                   4033:         } elsif ($space eq 'domain') {
1.218     albertel 4034:             return $udom;
1.48      www      4035: # ------------------------------------------------------------------- user.name
                   4036:         } elsif ($space eq 'name') {
1.218     albertel 4037:             return $uname;
1.48      www      4038: # ---------------------------------------------------- Any other user namespace
1.29      www      4039:         } else {
1.359     albertel 4040: 	    my %reply;
                   4041: 	    if (!$publicuser) {
                   4042: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   4043: 	    }
                   4044: 	    return $reply{$qualifierrest};
1.48      www      4045:         }
1.236     www      4046:     } elsif ($realm eq 'query') {
                   4047: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 4048:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   4049: 						[$spacequalifierrest]);
1.376     albertel 4050: 	return $ENV{'form.'.$spacequalifierrest}; 
1.236     www      4051:    } elsif ($realm eq 'request') {
1.48      www      4052: # ------------------------------------------------------------- request.browser
                   4053:         if ($space eq 'browser') {
1.430     www      4054: 	    if ($qualifier eq 'textremote') {
                   4055: 		if (&mt('textual_remote_display') eq 'on') {
                   4056: 		    return 1;
                   4057: 		} else {
                   4058: 		    return 0;
                   4059: 		}
                   4060: 	    } else {
                   4061: 		return $ENV{'browser.'.$qualifier};
                   4062: 	    }
1.57      www      4063: # ------------------------------------------------------------ request.filename
                   4064:         } else {
                   4065:             return $ENV{'request.'.$spacequalifierrest};
1.29      www      4066:         }
1.28      www      4067:     } elsif ($realm eq 'course') {
1.48      www      4068: # ---------------------------------------------------------- course.description
1.218     albertel 4069:         return $ENV{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      4070:     } elsif ($realm eq 'resource') {
1.165     www      4071: 
1.395     albertel 4072: 	my $section;
1.359     albertel 4073: 	if (defined($courseid) && $courseid eq $ENV{'request.course.id'}) {
1.539     albertel 4074: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   4075: 	}
                   4076: 	if ($symbparm && defined($courseid) && 
                   4077: 	    $courseid eq $ENV{'request.course.id'}) {
1.165     www      4078: 
1.218     albertel 4079: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      4080: 
1.60      www      4081: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 4082: 	    my $symbp=$symbparm;
1.409     www      4083: 	    my $mapp=(&decode_symb($symbp))[0];
1.218     albertel 4084: 
                   4085: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
                   4086: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
                   4087: 
                   4088: 	    if (($ENV{'user.name'} eq $uname) &&
                   4089: 		($ENV{'user.domain'} eq $udom)) {
1.255     albertel 4090: 		$section=$ENV{'request.course.sec'};
1.218     albertel 4091: 	    } else {
1.539     albertel 4092: 		if (! defined($usection)) {
                   4093: 		    $section=&usection($udom,$uname,$courseid);
                   4094: 		} else {
                   4095: 		    $section = $usection;
                   4096: 		}
1.218     albertel 4097: 	    }
                   4098: 
                   4099: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   4100: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
                   4101: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   4102: 
                   4103: 	    my $courselevel=$courseid.'.'.$spacequalifierrest;
                   4104: 	    my $courselevelr=$courseid.'.'.$symbparm;
                   4105: 	    my $courselevelm=$courseid.'.'.$mapparm;
1.69      www      4106: 
1.60      www      4107: # ----------------------------------------------------------- first, check user
1.379     matthew  4108: 	    #most student don\'t have any data set, check if there is some data
                   4109: 	    if (! &EXT_cache_status($udom,$uname)) {
1.420     albertel 4110: 		my $hashid="$udom:$uname";
1.425     albertel 4111: 		my ($result,$cached)=&is_cached(\%userresdatacache,$hashid,
                   4112: 						'userres');
1.454     albertel 4113: 		if (!defined($cached)) {
                   4114: 		    my %resourcedata=&dump('resourcedata',$udom,$uname);
1.420     albertel 4115: 		    $result=\%resourcedata;
1.425     albertel 4116: 		    &do_cache(\%userresdatacache,$hashid,$result,'userres');
1.420     albertel 4117: 		}
                   4118: 		my ($tmp)=keys(%$result);
1.308     albertel 4119: 		if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
1.420     albertel 4120: 		    if ($$result{$courselevelr}) {
                   4121: 			return $$result{$courselevelr}; }
                   4122: 		    if ($$result{$courselevelm}) {
                   4123: 			return $$result{$courselevelm}; }
                   4124: 		    if ($$result{$courselevel}) {
                   4125: 			return $$result{$courselevel}; }
1.308     albertel 4126: 		} else {
1.459     albertel 4127: 		    #error 2 occurs when the .db doesn't exist
                   4128: 		    if ($tmp!~/error: 2 /) {
1.308     albertel 4129: 			&logthis("<font color=blue>WARNING:".
                   4130: 				 " Trying to get resource data for ".
                   4131: 				 $uname." at ".$udom.": ".
                   4132: 				 $tmp."</font>");
1.459     albertel 4133: 		    } elsif ($tmp=~/error: 2 /) {
1.539     albertel 4134: 			&EXT_cache_set($udom,$uname);
1.308     albertel 4135: 		    } elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   4136: 			return $tmp;
                   4137: 		    }
1.218     albertel 4138: 		}
                   4139: 	    }
1.95      www      4140: 
1.60      www      4141: # -------------------------------------------------------- second, check course
1.96      www      4142: 
1.218     albertel 4143: 	    my $coursereply=&courseresdata($ENV{'course.'.$courseid.'.num'},
1.539     albertel 4144: 					   $ENV{'course.'.$courseid.'.domain'},
                   4145: 					   ($seclevelr,$seclevelm,$seclevel,
                   4146: 					    $courselevelr,$courselevelm,
                   4147: 					    $courselevel));
1.287     albertel 4148: 	    if (defined($coursereply)) { return $coursereply; }
1.200     www      4149: 
1.60      www      4150: # ------------------------------------------------------ third, check map parms
1.218     albertel 4151: 	    my %parmhash=();
                   4152: 	    my $thisparm='';
                   4153: 	    if (tie(%parmhash,'GDBM_File',
                   4154: 		    $ENV{'request.course.fn'}.'_parms.db',
1.256     albertel 4155: 		    &GDBM_READER(),0640)) {
1.218     albertel 4156: 		$thisparm=$parmhash{$symbparm};
                   4157: 		untie(%parmhash);
                   4158: 	    }
                   4159: 	    if ($thisparm) { return $thisparm; }
                   4160: 	}
1.60      www      4161: # --------------------------------------------- last, look in resource metadata
1.71      www      4162: 
1.218     albertel 4163: 	$spacequalifierrest=~s/\./\_/;
1.282     albertel 4164: 	my $filename;
                   4165: 	if (!$symbparm) { $symbparm=&symbread(); }
                   4166: 	if ($symbparm) {
1.409     www      4167: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 4168: 	} else {
                   4169: 	    $filename=$ENV{'request.filename'};
                   4170: 	}
                   4171: 	my $metadata=&metadata($filename,$spacequalifierrest);
1.288     albertel 4172: 	if (defined($metadata)) { return $metadata; }
1.282     albertel 4173: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.288     albertel 4174: 	if (defined($metadata)) { return $metadata; }
1.142     www      4175: 
1.145     www      4176: # ------------------------------------------------------------------ Cascade up
1.218     albertel 4177: 	unless ($space eq '0') {
1.336     albertel 4178: 	    my @parts=split(/_/,$space);
                   4179: 	    my $id=pop(@parts);
                   4180: 	    my $part=join('_',@parts);
                   4181: 	    if ($part eq '') { $part='0'; }
                   4182: 	    my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 4183: 				 $symbparm,$udom,$uname,$section,1);
1.337     albertel 4184: 	    if (defined($partgeneral)) { return $partgeneral; }
1.218     albertel 4185: 	}
1.395     albertel 4186: 	if ($recurse) { return undef; }
                   4187: 	my $pack_def=&packages_tab_default($filename,$varname);
                   4188: 	if (defined($pack_def)) { return $pack_def; }
1.71      www      4189: 
1.48      www      4190: # ---------------------------------------------------- Any other user namespace
                   4191:     } elsif ($realm eq 'environment') {
                   4192: # ----------------------------------------------------------------- environment
1.219     albertel 4193: 	if (($uname eq $ENV{'user.name'})&&($udom eq $ENV{'user.domain'})) {
                   4194: 	    return $ENV{'environment.'.$spacequalifierrest};
                   4195: 	} else {
                   4196: 	    my %returnhash=&userenvironment($udom,$uname,
                   4197: 					    $spacequalifierrest);
                   4198: 	    return $returnhash{$spacequalifierrest};
                   4199: 	}
1.28      www      4200:     } elsif ($realm eq 'system') {
1.48      www      4201: # ----------------------------------------------------------------- system.time
                   4202: 	if ($space eq 'time') {
                   4203: 	    return time;
                   4204:         }
1.28      www      4205:     }
1.48      www      4206:     return '';
1.61      www      4207: }
                   4208: 
1.395     albertel 4209: sub packages_tab_default {
                   4210:     my ($uri,$varname)=@_;
                   4211:     my (undef,$part,$name)=split(/\./,$varname);
                   4212:     my $packages=&metadata($uri,'packages');
                   4213:     foreach my $package (split(/,/,$packages)) {
                   4214: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.468     albertel 4215: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   4216: 	    return $packagetab{"$pack_type&$name&default"};
                   4217: 	}
                   4218: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   4219: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 4220: 	}
                   4221:     }
                   4222:     return undef;
                   4223: }
                   4224: 
1.334     albertel 4225: sub add_prefix_and_part {
                   4226:     my ($prefix,$part)=@_;
                   4227:     my $keyroot;
                   4228:     if (defined($prefix) && $prefix !~ /^__/) {
                   4229: 	# prefix that has a part already
                   4230: 	$keyroot=$prefix;
                   4231:     } elsif (defined($prefix)) {
                   4232: 	# prefix that is missing a part
                   4233: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   4234:     } else {
                   4235: 	# no prefix at all
                   4236: 	if (defined($part)) { $keyroot='_'.$part; }
                   4237:     }
                   4238:     return $keyroot;
                   4239: }
                   4240: 
1.71      www      4241: # ---------------------------------------------------------------- Get metadata
                   4242: 
                   4243: sub metadata {
1.176     www      4244:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71      www      4245:     $uri=&declutter($uri);
1.288     albertel 4246:     # if it is a non metadata possible uri return quickly
1.529     albertel 4247:     if (($uri eq '') || 
                   4248: 	(($uri =~ m|^/*adm/|) && 
                   4249: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.423     albertel 4250:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ /^~/) ||
1.489     albertel 4251: 	($uri =~ m|home/[^/]+/public_html/|)) {
1.468     albertel 4252: 	return undef;
1.288     albertel 4253:     }
1.73      www      4254:     my $filename=$uri;
                   4255:     $uri=~s/\.meta$//;
1.172     www      4256: #
                   4257: # Is the metadata already cached?
1.177     www      4258: # Look at timestamp of caching
1.172     www      4259: # Everything is cached by the main uri, libraries are never directly cached
                   4260: #
1.428     albertel 4261:     if (!defined($liburi)) {
                   4262: 	my ($result,$cached)=&is_cached(\%metacache,$uri,'meta');
                   4263: 	if (defined($cached)) { return $result->{':'.$what}; }
                   4264:     }
                   4265:     {
1.172     www      4266: #
                   4267: # Is this a recursive call for a library?
                   4268: #
1.453     albertel 4269: 	if (! exists($metacache{$uri})) {
                   4270: 	    $metacache{$uri}={};
                   4271: 	}
1.171     www      4272:         if ($liburi) {
                   4273: 	    $liburi=&declutter($liburi);
                   4274:             $filename=$liburi;
1.401     bowersj2 4275:         } else {
1.428     albertel 4276: 	    &devalidate_cache(\%metacache,$uri,'meta');
1.401     bowersj2 4277: 	}
1.140     www      4278:         my %metathesekeys=();
1.73      www      4279:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 4280: 	my $metastring;
                   4281: 	if ($uri !~ m|^uploaded/|) {
1.543   ! albertel 4282: 	    my $file=&filelocation('',&clutter($filename));
        !          4283: 	    push(@{$metacache{$uri.'.file'}},$file);
        !          4284: 	    $metastring=&getfile($file);
1.489     albertel 4285: 	}
1.208     albertel 4286:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      4287:         my $token;
1.140     www      4288:         undef %metathesekeys;
1.71      www      4289:         while ($token=$parser->get_token) {
1.339     albertel 4290: 	    if ($token->[0] eq 'S') {
                   4291: 		if (defined($token->[2]->{'package'})) {
1.172     www      4292: #
                   4293: # This is a package - get package info
                   4294: #
1.339     albertel 4295: 		    my $package=$token->[2]->{'package'};
                   4296: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   4297: 		    if (defined($token->[2]->{'id'})) { 
                   4298: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   4299: 		    }
1.453     albertel 4300: 		    if ($metacache{$uri}->{':packages'}) {
                   4301: 			$metacache{$uri}->{':packages'}.=','.$package.$keyroot;
1.339     albertel 4302: 		    } else {
1.453     albertel 4303: 			$metacache{$uri}->{':packages'}=$package.$keyroot;
1.339     albertel 4304: 		    }
                   4305: 		    foreach (keys %packagetab) {
1.432     albertel 4306: 			my $part=$keyroot;
                   4307: 			$part=~s/^\_//;
                   4308: 			if ($_=~/^\Q$package\E\&/ || 
                   4309: 			    $_=~/^\Q$package\E_0\&/) {
1.339     albertel 4310: 			    my ($pack,$name,$subp)=split(/\&/,$_);
1.395     albertel 4311: 			    # ignore package.tab specified default values
                   4312:                             # here &package_tab_default() will fetch those
                   4313: 			    if ($subp eq 'default') { next; }
1.339     albertel 4314: 			    my $value=$packagetab{$_};
1.432     albertel 4315: 			    my $unikey;
                   4316: 			    if ($pack =~ /_0$/) {
                   4317: 				$unikey='parameter_0_'.$name;
                   4318: 				$part=0;
                   4319: 			    } else {
                   4320: 				$unikey='parameter'.$keyroot.'_'.$name;
                   4321: 			    }
1.339     albertel 4322: 			    if ($subp eq 'display') {
                   4323: 				$value.=' [Part: '.$part.']';
                   4324: 			    }
1.453     albertel 4325: 			    $metacache{$uri}->{':'.$unikey.'.part'}=$part;
1.395     albertel 4326: 			    $metathesekeys{$unikey}=1;
1.453     albertel 4327: 			    unless (defined($metacache{$uri}->{':'.$unikey.'.'.$subp})) {
                   4328: 				$metacache{$uri}->{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 4329: 			    }
1.453     albertel 4330: 			    if (defined($metacache{$uri}->{':'.$unikey.'.default'})) {
                   4331: 				$metacache{$uri}->{':'.$unikey}=
                   4332: 				    $metacache{$uri}->{':'.$unikey.'.default'};
1.356     albertel 4333: 			    }
1.339     albertel 4334: 			}
                   4335: 		    }
                   4336: 		} else {
1.172     www      4337: #
                   4338: # This is not a package - some other kind of start tag
1.339     albertel 4339: #
                   4340: 		    my $entry=$token->[1];
                   4341: 		    my $unikey;
                   4342: 		    if ($entry eq 'import') {
                   4343: 			$unikey='';
                   4344: 		    } else {
                   4345: 			$unikey=$entry;
                   4346: 		    }
                   4347: 		    $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   4348: 
                   4349: 		    if (defined($token->[2]->{'id'})) { 
                   4350: 			$unikey.='_'.$token->[2]->{'id'}; 
                   4351: 		    }
1.175     www      4352: 
1.339     albertel 4353: 		    if ($entry eq 'import') {
1.175     www      4354: #
                   4355: # Importing a library here
1.339     albertel 4356: #
                   4357: 			if ($depthcount<20) {
                   4358: 			    my $location=$parser->get_text('/import');
                   4359: 			    my $dir=$filename;
                   4360: 			    $dir=~s|[^/]*$||;
                   4361: 			    $location=&filelocation($dir,$location);
                   4362: 			    foreach (sort(split(/\,/,&metadata($uri,'keys',
                   4363: 							       $location,$unikey,
                   4364: 							       $depthcount+1)))) {
1.453     albertel 4365: 				$metacache{$uri}->{':'.$_}=$metacache{$uri}->{':'.$_};
1.339     albertel 4366: 				$metathesekeys{$_}=1;
                   4367: 			    }
                   4368: 			}
                   4369: 		    } else { 
                   4370: 			
                   4371: 			if (defined($token->[2]->{'name'})) { 
                   4372: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   4373: 			}
                   4374: 			$metathesekeys{$unikey}=1;
                   4375: 			foreach (@{$token->[3]}) {
1.453     albertel 4376: 			    $metacache{$uri}->{':'.$unikey.'.'.$_}=$token->[2]->{$_};
1.339     albertel 4377: 			}
                   4378: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.453     albertel 4379: 			my $default=$metacache{$uri}->{':'.$unikey.'.default'};
1.339     albertel 4380: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   4381: 		 # only ws inside the tag, and not in default, so use default
                   4382: 		 # as value
1.453     albertel 4383: 			    $metacache{$uri}->{':'.$unikey}=$default;
1.339     albertel 4384: 			} else {
1.321     albertel 4385: 		  # either something interesting inside the tag or default
                   4386:                   # uninteresting
1.453     albertel 4387: 			    $metacache{$uri}->{':'.$unikey}=$internaltext;
1.339     albertel 4388: 			}
1.172     www      4389: # end of not-a-package not-a-library import
1.339     albertel 4390: 		    }
1.172     www      4391: # end of not-a-package start tag
1.339     albertel 4392: 		}
1.172     www      4393: # the next is the end of "start tag"
1.339     albertel 4394: 	    }
                   4395: 	}
1.483     albertel 4396: 	my ($extension) = ($uri =~ /\.(\w+)$/);
                   4397: 	foreach my $key (sort(keys(%packagetab))) {
                   4398: 	    #&logthis("extsion1 $extension $key !!");
                   4399: 	    #no specific packages #how's our extension
                   4400: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 4401: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 4402: 					 \%metathesekeys);
                   4403: 	}
                   4404: 	if (!exists($metacache{$uri}->{':packages'})) {
                   4405: 	    foreach my $key (sort(keys(%packagetab))) {
                   4406: 		#no specific packages well let's get default then
                   4407: 		if ($key!~/^default&/) { next; }
1.488     albertel 4408: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 4409: 					     \%metathesekeys);
                   4410: 	    }
                   4411: 	}
1.338     www      4412: # are there custom rights to evaluate
1.453     albertel 4413: 	if ($metacache{$uri}->{':copyright'} eq 'custom') {
1.339     albertel 4414: 
1.338     www      4415:     #
                   4416:     # Importing a rights file here
1.339     albertel 4417:     #
                   4418: 	    unless ($depthcount) {
1.453     albertel 4419: 		my $location=$metacache{$uri}->{':customdistributionfile'};
1.339     albertel 4420: 		my $dir=$filename;
                   4421: 		$dir=~s|[^/]*$||;
                   4422: 		$location=&filelocation($dir,$location);
                   4423: 		foreach (sort(split(/\,/,&metadata($uri,'keys',
                   4424: 						   $location,'_rights',
                   4425: 						   $depthcount+1)))) {
1.453     albertel 4426: 		    $metacache{$uri}->{':'.$_}=$metacache{$uri}->{':'.$_};
1.339     albertel 4427: 		    $metathesekeys{$_}=1;
                   4428: 		}
                   4429: 	    }
                   4430: 	}
1.453     albertel 4431: 	$metacache{$uri}->{':keys'}=join(',',keys %metathesekeys);
                   4432: 	&metadata_generate_part0(\%metathesekeys,$metacache{$uri},$uri);
                   4433: 	$metacache{$uri}->{':allpossiblekeys'}=join(',',keys %metathesekeys);
                   4434: 	&do_cache(\%metacache,$uri,$metacache{$uri},'meta');
1.177     www      4435: # this is the end of "was not already recently cached
1.71      www      4436:     }
1.428     albertel 4437:     return $metacache{$uri}->{':'.$what};
1.261     albertel 4438: }
                   4439: 
1.488     albertel 4440: sub metadata_create_package_def {
1.483     albertel 4441:     my ($uri,$key,$package,$metathesekeys)=@_;
                   4442:     my ($pack,$name,$subp)=split(/\&/,$key);
                   4443:     if ($subp eq 'default') { next; }
                   4444:     
                   4445:     if (defined($metacache{$uri}->{':packages'})) {
                   4446: 	$metacache{$uri}->{':packages'}.=','.$package;
                   4447:     } else {
                   4448: 	$metacache{$uri}->{':packages'}=$package;
                   4449:     }
                   4450:     my $value=$packagetab{$key};
                   4451:     my $unikey;
                   4452:     $unikey='parameter_0_'.$name;
                   4453:     $metacache{$uri}->{':'.$unikey.'.part'}=0;
                   4454:     $$metathesekeys{$unikey}=1;
                   4455:     unless (defined($metacache{$uri}->{':'.$unikey.'.'.$subp})) {
                   4456: 	$metacache{$uri}->{':'.$unikey.'.'.$subp}=$value;
                   4457:     }
                   4458:     if (defined($metacache{$uri}->{':'.$unikey.'.default'})) {
                   4459: 	$metacache{$uri}->{':'.$unikey}=
                   4460: 	    $metacache{$uri}->{':'.$unikey.'.default'};
                   4461:     }
                   4462: }
                   4463: 
1.261     albertel 4464: sub metadata_generate_part0 {
                   4465:     my ($metadata,$metacache,$uri) = @_;
                   4466:     my %allnames;
                   4467:     foreach my $metakey (sort keys %$metadata) {
                   4468: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 4469: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   4470: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 4471: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 4472: 	    $allnames{$name}=$part;
                   4473: 	  }
                   4474: 	}
                   4475:     }
                   4476:     foreach my $name (keys(%allnames)) {
                   4477:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 4478:       my $key=":parameter_0_$name";
1.261     albertel 4479:       $$metacache{"$key.part"}='0';
                   4480:       $$metacache{"$key.name"}=$name;
1.428     albertel 4481:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 4482: 					   $allnames{$name}.'_'.$name.
                   4483: 					   '.type'};
1.428     albertel 4484:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 4485: 			     '.display'};
                   4486:       my $expr='\\[Part: '.$allnames{$name}.'\\]';
1.479     albertel 4487:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 4488:       $$metacache{"$key.display"}=$olddis;
                   4489:     }
1.71      www      4490: }
                   4491: 
1.301     www      4492: # ------------------------------------------------- Get the title of a resource
                   4493: 
                   4494: sub gettitle {
                   4495:     my $urlsymb=shift;
                   4496:     my $symb=&symbread($urlsymb);
1.534     albertel 4497:     if ($symb) {
                   4498: 	my ($result,$cached)=&is_cached(\%titlecache,$symb,'title',600);
                   4499: 	if (defined($cached)) { return $result; }
                   4500: 	my ($map,$resid,$url)=&decode_symb($symb);
                   4501: 	my $title='';
                   4502: 	my %bighash;
                   4503: 	if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
                   4504: 		&GDBM_READER(),0640)) {
                   4505: 	    my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   4506: 	    $title=$bighash{'title_'.$mapid.'.'.$resid};
                   4507: 	    untie %bighash;
                   4508: 	}
                   4509: 	$title=~s/\&colon\;/\:/gs;
                   4510: 	if ($title) {
                   4511: 	    return &do_cache(\%titlecache,$symb,$title,'title');
                   4512: 	}
                   4513: 	$urlsymb=$url;
                   4514:     }
                   4515:     my $title=&metadata($urlsymb,'title');
                   4516:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   4517:     return $title;
1.301     www      4518: }
                   4519:     
1.31      www      4520: # ------------------------------------------------- Update symbolic store links
                   4521: 
                   4522: sub symblist {
                   4523:     my ($mapname,%newhash)=@_;
1.438     www      4524:     $mapname=&deversion(&declutter($mapname));
1.31      www      4525:     my %hash;
                   4526:     if (($ENV{'request.course.fn'}) && (%newhash)) {
                   4527:         if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.256     albertel 4528:                       &GDBM_WRCREAT(),0640)) {
1.191     harris41 4529: 	    foreach (keys %newhash) {
1.438     www      4530:                 $hash{declutter($_)}=$mapname.'___'.&deversion($newhash{$_});
1.191     harris41 4531:             }
1.31      www      4532:             if (untie(%hash)) {
                   4533: 		return 'ok';
                   4534:             }
                   4535:         }
                   4536:     }
                   4537:     return 'error';
1.212     www      4538: }
                   4539: 
                   4540: # --------------------------------------------------------------- Verify a symb
                   4541: 
                   4542: sub symbverify {
1.510     www      4543:     my ($symb,$thisurl)=@_;
                   4544:     my $thisfn=$thisurl;
                   4545: # wrapper not part of symbs
                   4546:     $thisfn=~s/^\/adm\/wrapper//;
1.439     www      4547:     $thisfn=&declutter($thisfn);
1.215     www      4548: # direct jump to resource in page or to a sequence - will construct own symbs
                   4549:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   4550: # check URL part
1.409     www      4551:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      4552: 
1.431     www      4553:     unless ($url eq $thisfn) { return 0; }
1.213     www      4554: 
1.216     www      4555:     $symb=&symbclean($symb);
1.510     www      4556:     $thisurl=&deversion($thisurl);
1.439     www      4557:     $thisfn=&deversion($thisfn);
1.213     www      4558: 
                   4559:     my %bighash;
                   4560:     my $okay=0;
1.431     www      4561: 
1.213     www      4562:     if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.256     albertel 4563:                             &GDBM_READER(),0640)) {
1.510     www      4564:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216     www      4565:         unless ($ids) { 
1.510     www      4566:            $ids=$bighash{'ids_/'.$thisurl};
1.216     www      4567:         }
                   4568:         if ($ids) {
                   4569: # ------------------------------------------------------------------- Has ID(s)
                   4570: 	    foreach (split(/\,/,$ids)) {
                   4571:                my ($mapid,$resid)=split(/\./,$_);
                   4572:                if (
                   4573:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
                   4574:    eq $symb) { 
                   4575:                   $okay=1; 
                   4576:                }
                   4577: 	   }
                   4578:         }
1.213     www      4579: 	untie(%bighash);
                   4580:     }
                   4581:     return $okay;
1.31      www      4582: }
                   4583: 
1.210     www      4584: # --------------------------------------------------------------- Clean-up symb
                   4585: 
                   4586: sub symbclean {
                   4587:     my $symb=shift;
1.213     www      4588: 
1.210     www      4589: # remove version from map
                   4590:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      4591: 
1.210     www      4592: # remove version from URL
                   4593:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      4594: 
1.507     www      4595: # remove wrapper
                   4596: 
1.510     www      4597:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.210     www      4598:     return $symb;
1.409     www      4599: }
                   4600: 
                   4601: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 4602: 
                   4603: sub encode_symb {
                   4604:     my ($map,$resid,$url)=@_;
                   4605:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   4606: }
1.409     www      4607: 
                   4608: sub decode_symb {
1.413     www      4609:     my ($map,$resid,$url)=split(/\_\_\_/,shift);
                   4610:     return (&fixversion($map),$resid,&fixversion($url));
                   4611: }
                   4612: 
                   4613: sub fixversion {
                   4614:     my $fn=shift;
                   4615:     if ($fn=~/^(adm|uploaded|public)/) { return $fn; }
1.435     www      4616:     my %bighash;
                   4617:     my $uri=&clutter($fn);
1.440     www      4618:     my $key=$ENV{'request.course.id'}.'_'.$uri;
                   4619: # is this cached?
                   4620:     my ($result,$cached)=&is_cached(\%courseresversioncache,$key,
                   4621: 				    'courseresversion',600);
                   4622:     if (defined($cached)) { return $result; }
                   4623: # unfortunately not cached, or expired
1.435     www      4624:     if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.440     www      4625: 	    &GDBM_READER(),0640)) {
                   4626:  	if ($bighash{'version_'.$uri}) {
                   4627:  	    my $version=$bighash{'version_'.$uri};
1.444     www      4628:  	    unless (($version eq 'mostrecent') || 
                   4629: 		    ($version==&getversion($uri))) {
1.440     www      4630:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   4631:  	    }
                   4632:  	}
                   4633:  	untie %bighash;
1.413     www      4634:     }
1.440     www      4635:     return &do_cache
                   4636: 	(\%courseresversioncache,$key,&declutter($uri),'courseresversion');
1.438     www      4637: }
                   4638: 
                   4639: sub deversion {
                   4640:     my $url=shift;
                   4641:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   4642:     return $url;
1.210     www      4643: }
                   4644: 
1.31      www      4645: # ------------------------------------------------------ Return symb list entry
                   4646: 
                   4647: sub symbread {
1.249     www      4648:     my ($thisfn,$donotrecurse)=@_;
1.542     albertel 4649:     my $cache_str='request.symbread.cached.'.$thisfn;
                   4650:     if (defined($ENV{$cache_str})) { return $ENV{$cache_str}; }
1.242     www      4651: # no filename provided? try from environment
1.44      www      4652:     unless ($thisfn) {
1.539     albertel 4653:         if ($ENV{'request.symb'}) {
1.542     albertel 4654: 	    return $ENV{$cache_str}=&symbclean($ENV{'request.symb'});
1.539     albertel 4655: 	}
1.44      www      4656: 	$thisfn=$ENV{'request.filename'};
                   4657:     }
1.242     www      4658: # is that filename actually a symb? Verify, clean, and return
                   4659:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 4660: 	if (&symbverify($thisfn,$1)) {
1.542     albertel 4661: 	    return $ENV{$cache_str}=&symbclean($thisfn);
1.539     albertel 4662: 	}
1.242     www      4663:     }
1.44      www      4664:     $thisfn=declutter($thisfn);
1.31      www      4665:     my %hash;
1.37      www      4666:     my %bighash;
                   4667:     my $syval='';
1.45      www      4668:     if (($ENV{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  4669:         my $targetfn = $thisfn;
                   4670:         if ( ($thisfn =~ m/^uploaded\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
                   4671:             $targetfn = 'adm/wrapper/'.$thisfn;
                   4672:         }
1.31      www      4673:         if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.256     albertel 4674:                       &GDBM_READER(),0640)) {
1.481     raeburn  4675: 	    $syval=$hash{$targetfn};
1.37      www      4676:             untie(%hash);
                   4677:         }
                   4678: # ---------------------------------------------------------- There was an entry
                   4679:         if ($syval) {
                   4680:            unless ($syval=~/\_\d+$/) {
                   4681: 	       unless ($ENV{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.44      www      4682:                   &appenv('request.ambiguous' => $thisfn);
1.542     albertel 4683: 		  return $ENV{$cache_str}='';
1.37      www      4684:                }    
                   4685:                $syval.=$1;
                   4686: 	   }
                   4687:         } else {
                   4688: # ------------------------------------------------------- Was not in symb table
                   4689:            if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.256     albertel 4690:                             &GDBM_READER(),0640)) {
1.37      www      4691: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      4692:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      4693:               unless ($ids) { 
                   4694:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      4695:               }
                   4696:               unless ($ids) {
                   4697: # alias?
                   4698: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      4699:               }
1.37      www      4700:               if ($ids) {
                   4701: # ------------------------------------------------------------------- Has ID(s)
                   4702:                  my @possibilities=split(/\,/,$ids);
1.39      www      4703:                  if ($#possibilities==0) {
                   4704: # ----------------------------------------------- There is only one possibility
1.37      www      4705: 		     my ($mapid,$resid)=split(/\./,$ids);
                   4706:                      $syval=declutter($bighash{'map_id_'.$mapid}).'___'.$resid;
1.249     www      4707:                  } elsif (!$donotrecurse) {
1.39      www      4708: # ------------------------------------------ There is more than one possibility
                   4709:                      my $realpossible=0;
1.191     harris41 4710:                      foreach (@possibilities) {
1.39      www      4711: 			 my $file=$bighash{'src_'.$_};
                   4712:                          if (&allowed('bre',$file)) {
                   4713:          		    my ($mapid,$resid)=split(/\./,$_);
                   4714:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
                   4715: 				$realpossible++;
                   4716:                                 $syval=declutter($bighash{'map_id_'.$mapid}).
                   4717:                                        '___'.$resid;
                   4718:                             }
                   4719: 			 }
1.191     harris41 4720:                      }
1.39      www      4721: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      4722:                  } else {
                   4723:                      $syval='';
1.37      www      4724:                  }
                   4725: 	      }
                   4726:               untie(%bighash)
1.481     raeburn  4727:            }
1.31      www      4728:         }
1.62      www      4729:         if ($syval) {
1.542     albertel 4730: 	    return $ENV{$cache_str}=&symbclean($syval.'___'.$thisfn);
1.62      www      4731:         }
1.31      www      4732:     }
1.44      www      4733:     &appenv('request.ambiguous' => $thisfn);
1.542     albertel 4734:     return $ENV{$cache_str}='';
1.31      www      4735: }
                   4736: 
                   4737: # ---------------------------------------------------------- Return random seed
                   4738: 
1.32      www      4739: sub numval {
                   4740:     my $txt=shift;
                   4741:     $txt=~tr/A-J/0-9/;
                   4742:     $txt=~tr/a-j/0-9/;
                   4743:     $txt=~tr/K-T/0-9/;
                   4744:     $txt=~tr/k-t/0-9/;
                   4745:     $txt=~tr/U-Z/0-5/;
                   4746:     $txt=~tr/u-z/0-5/;
                   4747:     $txt=~s/\D//g;
                   4748:     return int($txt);
1.368     albertel 4749: }
                   4750: 
1.484     albertel 4751: sub numval2 {
                   4752:     my $txt=shift;
                   4753:     $txt=~tr/A-J/0-9/;
                   4754:     $txt=~tr/a-j/0-9/;
                   4755:     $txt=~tr/K-T/0-9/;
                   4756:     $txt=~tr/k-t/0-9/;
                   4757:     $txt=~tr/U-Z/0-5/;
                   4758:     $txt=~tr/u-z/0-5/;
                   4759:     $txt=~s/\D//g;
                   4760:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   4761:     my $total;
                   4762:     foreach my $val (@txts) { $total+=$val; }
                   4763:     return int($total);
                   4764: }
                   4765: 
1.368     albertel 4766: sub latest_rnd_algorithm_id {
1.501     albertel 4767:     return '64bit3';
1.366     albertel 4768: }
1.32      www      4769: 
1.503     albertel 4770: sub get_rand_alg {
                   4771:     my ($courseid)=@_;
                   4772:     if (!$courseid) { $courseid=(&Apache::lonxml::whichuser())[1]; }
                   4773:     if ($courseid) {
                   4774: 	return $ENV{"course.$courseid.rndseed"};
                   4775:     }
                   4776:     return &latest_rnd_algorithm_id();
                   4777: }
                   4778: 
1.491     albertel 4779: sub getCODE {
                   4780:     if (defined($ENV{'form.CODE'})) { return $ENV{'form.CODE'}; }
                   4781:     if (defined($Apache::lonhomework::parsing_a_problem) &&
                   4782: 	defined($Apache::lonhomework::history{'resource.CODE'})) {
                   4783: 	return $Apache::lonhomework::history{'resource.CODE'};
                   4784:     }
                   4785:     return undef;
                   4786: }
                   4787: 
1.31      www      4788: sub rndseed {
1.155     albertel 4789:     my ($symb,$courseid,$domain,$username)=@_;
1.366     albertel 4790: 
                   4791:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&Apache::lonxml::whichuser();
1.155     albertel 4792:     if (!$symb) {
1.366     albertel 4793: 	unless ($symb=$wsymb) { return time; }
                   4794:     }
                   4795:     if (!$courseid) { $courseid=$wcourseid; }
                   4796:     if (!$domain) { $domain=$wdomain; }
                   4797:     if (!$username) { $username=$wusername }
1.503     albertel 4798:     my $which=&get_rand_alg();
1.491     albertel 4799:     if (defined(&getCODE())) {
1.484     albertel 4800: 	return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
1.501     albertel 4801:     } elsif ($which eq '64bit3') {
                   4802: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 4803:     } elsif ($which eq '64bit2') {
                   4804: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 4805:     } elsif ($which eq '64bit') {
                   4806: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   4807:     }
                   4808:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   4809: }
                   4810: 
                   4811: sub rndseed_32bit {
                   4812:     my ($symb,$courseid,$domain,$username)=@_;
                   4813:     {
                   4814: 	use integer;
                   4815: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   4816: 	my $symbseed=numval($symb) << 22;
                   4817: 	my $namechck=unpack("%32C*",$username) << 17;
                   4818: 	my $nameseed=numval($username) << 12;
                   4819: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   4820: 	my $courseseed=unpack("%32C*",$courseid);
                   4821: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
                   4822: 	#&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   4823: 	#&Apache::lonxml::debug("rndseed :$num:$symb");
                   4824: 	return $num;
                   4825:     }
                   4826: }
                   4827: 
                   4828: sub rndseed_64bit {
                   4829:     my ($symb,$courseid,$domain,$username)=@_;
                   4830:     {
                   4831: 	use integer;
                   4832: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   4833: 	my $symbseed=numval($symb) << 10;
                   4834: 	my $namechck=unpack("%32S*",$username);
                   4835: 	
                   4836: 	my $nameseed=numval($username) << 21;
                   4837: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   4838: 	my $courseseed=unpack("%32S*",$courseid);
                   4839: 	
                   4840: 	my $num1=$symbchck+$symbseed+$namechck;
                   4841: 	my $num2=$nameseed+$domainseed+$courseseed;
                   4842: 	#&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   4843: 	#&Apache::lonxml::debug("rndseed :$num:$symb");
                   4844: 	return "$num1,$num2";
1.155     albertel 4845:     }
1.366     albertel 4846: }
                   4847: 
1.443     albertel 4848: sub rndseed_64bit2 {
                   4849:     my ($symb,$courseid,$domain,$username)=@_;
                   4850:     {
                   4851: 	use integer;
                   4852: 	# strings need to be an even # of cahracters long, it it is odd the
                   4853:         # last characters gets thrown away
                   4854: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   4855: 	my $symbseed=numval($symb) << 10;
                   4856: 	my $namechck=unpack("%32S*",$username.' ');
                   4857: 	
                   4858: 	my $nameseed=numval($username) << 21;
1.501     albertel 4859: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   4860: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   4861: 	
                   4862: 	my $num1=$symbchck+$symbseed+$namechck;
                   4863: 	my $num2=$nameseed+$domainseed+$courseseed;
                   4864: 	#&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   4865: 	#&Apache::lonxml::debug("rndseed :$num:$symb");
                   4866: 	return "$num1,$num2";
                   4867:     }
                   4868: }
                   4869: 
                   4870: sub rndseed_64bit3 {
                   4871:     my ($symb,$courseid,$domain,$username)=@_;
                   4872:     {
                   4873: 	use integer;
                   4874: 	# strings need to be an even # of cahracters long, it it is odd the
                   4875:         # last characters gets thrown away
                   4876: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   4877: 	my $symbseed=numval2($symb) << 10;
                   4878: 	my $namechck=unpack("%32S*",$username.' ');
                   4879: 	
                   4880: 	my $nameseed=numval2($username) << 21;
1.443     albertel 4881: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   4882: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   4883: 	
                   4884: 	my $num1=$symbchck+$symbseed+$namechck;
                   4885: 	my $num2=$nameseed+$domainseed+$courseseed;
                   4886: 	#&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   4887: 	#&Apache::lonxml::debug("rndseed :$num:$symb");
1.503     albertel 4888: 	return "$num1:$num2";
1.443     albertel 4889:     }
                   4890: }
                   4891: 
1.366     albertel 4892: sub rndseed_CODE_64bit {
                   4893:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 4894:     {
1.366     albertel 4895: 	use integer;
1.443     albertel 4896: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 4897: 	my $symbseed=numval2($symb);
1.491     albertel 4898: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   4899: 	my $CODEseed=numval(&getCODE());
1.443     albertel 4900: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 4901: 	my $num1=$symbseed+$CODEchck;
                   4902: 	my $num2=$CODEseed+$courseseed+$symbchck;
                   4903: 	#&Apache::lonxml::debug("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
1.366     albertel 4904: 	#&Apache::lonxml::debug("rndseed :$num1:$num2:$symb");
1.503     albertel 4905: 	return "$num1:$num2";
1.366     albertel 4906:     }
                   4907: }
                   4908: 
                   4909: sub setup_random_from_rndseed {
                   4910:     my ($rndseed)=@_;
1.503     albertel 4911:     if ($rndseed =~/([,:])/) {
                   4912: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366     albertel 4913: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
                   4914:     } else {
                   4915: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 4916:     }
1.36      albertel 4917: }
                   4918: 
1.474     albertel 4919: sub latest_receipt_algorithm_id {
                   4920:     return 'receipt2';
                   4921: }
                   4922: 
1.480     www      4923: sub recunique {
                   4924:     my $fucourseid=shift;
                   4925:     my $unique;
                   4926:     if ($ENV{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   4927: 	$unique=$ENV{"course.$fucourseid.internal.encseed"};
                   4928:     } else {
                   4929: 	$unique=$perlvar{'lonReceipt'};
                   4930:     }
                   4931:     return unpack("%32C*",$unique);
                   4932: }
                   4933: 
                   4934: sub recprefix {
                   4935:     my $fucourseid=shift;
                   4936:     my $prefix;
                   4937:     if ($ENV{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   4938: 	$prefix=$ENV{"course.$fucourseid.internal.encpref"};
                   4939:     } else {
                   4940: 	$prefix=$perlvar{'lonHostID'};
                   4941:     }
                   4942:     return unpack("%32C*",$prefix);
                   4943: }
                   4944: 
1.76      www      4945: sub ireceipt {
1.474     albertel 4946:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.76      www      4947:     my $cuname=unpack("%32C*",$funame);
                   4948:     my $cudom=unpack("%32C*",$fudom);
                   4949:     my $cucourseid=unpack("%32C*",$fucourseid);
                   4950:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      4951:     my $cunique=&recunique($fucourseid);
1.474     albertel 4952:     my $cpart=unpack("%32S*",$part);
1.480     www      4953:     my $return =&recprefix($fucourseid).'-';
1.474     albertel 4954:     if ($ENV{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   4955: 	$ENV{'request.state'} eq 'construct') {
                   4956: 	&Apache::lonxml::debug("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname).
                   4957: 			       " and ".($cpart%$cudom));
                   4958: 			       
                   4959: 	$return.= ($cunique%$cuname+
                   4960: 		   $cunique%$cudom+
                   4961: 		   $cusymb%$cuname+
                   4962: 		   $cusymb%$cudom+
                   4963: 		   $cucourseid%$cuname+
                   4964: 		   $cucourseid%$cudom+
                   4965: 		   $cpart%$cuname+
                   4966: 		   $cpart%$cudom);
                   4967:     } else {
                   4968: 	$return.= ($cunique%$cuname+
                   4969: 		   $cunique%$cudom+
                   4970: 		   $cusymb%$cuname+
                   4971: 		   $cusymb%$cudom+
                   4972: 		   $cucourseid%$cuname+
                   4973: 		   $cucourseid%$cudom);
                   4974:     }
                   4975:     return $return;
1.76      www      4976: }
                   4977: 
                   4978: sub receipt {
1.474     albertel 4979:     my ($part)=@_;
                   4980:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                   4981:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      4982: }
1.260     ng       4983: 
1.36      albertel 4984: # ------------------------------------------------------------ Serves up a file
1.472     albertel 4985: # returns either the contents of the file or 
                   4986: # -1 if the file doesn't exist
1.481     raeburn  4987: #
                   4988: # if the target is a file that was uploaded via DOCS, 
                   4989: # a check will be made to see if a current copy exists on the local server,
                   4990: # if it does this will be served, otherwise a copy will be retrieved from
                   4991: # the home server for the course and stored in /home/httpd/html/userfiles on
                   4992: # the local server.   
1.472     albertel 4993: 
1.36      albertel 4994: sub getfile {
1.538     albertel 4995:     my ($file) = @_;
1.482     albertel 4996: 
1.538     albertel 4997:     if ($file =~ m|^/*uploaded/|) { $file=&filelocation("",$file); }
                   4998:     &repcopy($file);
                   4999:     return &readfile($file);
                   5000: }
                   5001: 
                   5002: sub repcopy_userfile {
                   5003:     my ($file)=@_;
                   5004: 
                   5005:     if ($file =~ m|^/*uploaded/|) { $file=&filelocation("",$file); }
                   5006:     if ($file =~ m|^/home/httpd/html/lonUsers/|) { return OK; }
                   5007: 
                   5008:     my ($cdom,$cnum,$filename) = 
                   5009: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+([^/]+)/+([^/]+)/+(.*)|);
                   5010:     my ($info,$rtncode);
                   5011:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   5012:     if (-e "$file") {
                   5013: 	my @fileinfo = stat($file);
                   5014: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 5015: 	if ($lwpresp ne 'ok') {
                   5016: 	    if ($rtncode eq '404') {
1.538     albertel 5017: 		unlink($file);
1.482     albertel 5018: 	    }
1.517     albertel 5019: 	    #my $ua=new LWP::UserAgent;
1.538     albertel 5020: 	    #my $request=new HTTP::Request('GET',&tokenwrapper($uri));
1.517     albertel 5021: 	    #my $response=$ua->request($request);
                   5022: 	    #if ($response->is_success()) {
                   5023: 	#	return $response->content;
                   5024: 	#    } else {
                   5025: 	#	return -1;
                   5026: 	#    }
1.482     albertel 5027: 	    return -1;
                   5028: 	}
                   5029: 	if ($info < $fileinfo[9]) {
1.538     albertel 5030: 	    return OK;
1.482     albertel 5031: 	}
                   5032: 	$info = '';
1.538     albertel 5033: 	$lwpresp = &getuploaded('GET',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 5034: 	if ($lwpresp ne 'ok') {
                   5035: 	    return -1;
                   5036: 	}
                   5037:     } else {
1.538     albertel 5038: 	my $lwpresp = &getuploaded('GET',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 5039: 	if ($lwpresp ne 'ok') {
1.517     albertel 5040: 	    my $ua=new LWP::UserAgent;
1.538     albertel 5041: 	    my $request=new HTTP::Request('GET',&tokenwrapper($uri));
1.517     albertel 5042: 	    my $response=$ua->request($request);
                   5043: 	    if ($response->is_success()) {
1.538     albertel 5044: 		$info=$response->content;
1.517     albertel 5045: 	    } else {
                   5046: 		return -1;
                   5047: 	    }
1.482     albertel 5048: 	}
                   5049: 	my @parts = ($cdom,$cnum); 
                   5050: 	if ($filename =~ m|^(.+)/[^/]+$|) {
                   5051: 	    push @parts, split(/\//,$1);
1.518     albertel 5052: 	}
1.538     albertel 5053: 	my $path = $perlvar{'lonDocRoot'}.'/userfiles';
1.482     albertel 5054: 	foreach my $part (@parts) {
                   5055: 	    $path .= '/'.$part;
                   5056: 	    if (!-e $path) {
                   5057: 		mkdir($path,0770);
                   5058: 	    }
                   5059: 	}
                   5060:     }
1.538     albertel 5061:     open(FILE,">$file");
1.482     albertel 5062:     print FILE $info;
                   5063:     close(FILE);
1.538     albertel 5064:     return OK;
1.481     raeburn  5065: }
                   5066: 
1.517     albertel 5067: sub tokenwrapper {
                   5068:     my $uri=shift;
                   5069:     $uri=~s/^http\:\/\/([^\/]+)//;
                   5070:     $uri=~s/^\///;
                   5071:     $ENV{'user.environment'}=~/\/([^\/]+)\.id/;
                   5072:     my $token=$1;
                   5073:     if ($uri=~/^uploaded\/([^\/]+)\/([^\/]+)\/([^\/]+)(\?\.*)*$/) {
                   5074:         &appenv('userfile.'.$1.'/'.$2.'/'.$3 => $ENV{'request.course.id'});
                   5075:         return 'http://'.$hostname{ &homeserver($2,$1)}.'/'.$uri.
                   5076:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   5077:                                '&tokenissued='.$perlvar{'lonHostID'};
                   5078:     } else {
                   5079:         return '/adm/notfound.html';
                   5080:     }
                   5081: }
                   5082: 
1.481     raeburn  5083: sub getuploaded {
                   5084:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   5085:     $uri=~s/^\///;
                   5086:     $uri = 'http://'.$hostname{ &homeserver($cnum,$cdom)}.'/raw/'.$uri;
                   5087:     my $ua=new LWP::UserAgent;
                   5088:     my $request=new HTTP::Request($reqtype,$uri);
                   5089:     my $response=$ua->request($request);
                   5090:     $$rtncode = $response->code;
1.482     albertel 5091:     if (! $response->is_success()) {
                   5092: 	return 'failed';
                   5093:     }      
                   5094:     if ($reqtype eq 'HEAD') {
1.486     www      5095: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 5096:     } elsif ($reqtype eq 'GET') {
                   5097: 	$$info = $response->content;
1.472     albertel 5098:     }
1.482     albertel 5099:     return 'ok';
1.36      albertel 5100: }
                   5101: 
1.481     raeburn  5102: sub readfile {
                   5103:     my $file = shift;
                   5104:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   5105:     my $fh;
                   5106:     open($fh,"<$file");
                   5107:     my $a='';
                   5108:     while (<$fh>) { $a .=$_; }
                   5109:     return $a;
                   5110: }
                   5111: 
1.36      albertel 5112: sub filelocation {
                   5113:   my ($dir,$file) = @_;
                   5114:   my $location;
                   5115:   $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.59      albertel 5116:   if ($file=~m:^/~:) { # is a contruction space reference
                   5117:     $location = $file;
                   5118:     $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.270     www      5119:   } elsif ($file=~/^\/*uploaded/) { # is an uploaded file
1.537     albertel 5120:       my ($udom,$uname,$filename)=
                   5121: 	  ($file=~m|^/+uploaded/+([^/]+)/+([^/]+)/+(.*)$|);
                   5122:       my $home=&homeserver($uname,$udom);
                   5123:       my $is_me=0;
1.536     sakharuk 5124:       my @ids=&current_machine_ids();
1.537     albertel 5125:       foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   5126:       if ($is_me) {
                   5127: 	  $location=&Apache::loncommon::propath($udom,$uname).
                   5128: 	      '/userfiles/'.$filename;
1.527     sakharuk 5129:       } else {
1.537     albertel 5130: 	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   5131: 	      $udom.'/'.$uname.'/'.$filename;
1.527     sakharuk 5132:       }
1.36      albertel 5133:   } else {
1.479     albertel 5134:     $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.464     albertel 5135:     $file=~s:^/res/:/:;
1.59      albertel 5136:     if ( !( $file =~ m:^/:) ) {
                   5137:       $location = $dir. '/'.$file;
                   5138:     } else {
                   5139:       $location = '/home/httpd/html/res'.$file;
                   5140:     }
1.36      albertel 5141:   }
                   5142:   $location=~s://+:/:g; # remove duplicate /
1.46      www      5143:   while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
1.475     albertel 5144:   while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
1.46      www      5145:   return $location;
                   5146: }
1.36      albertel 5147: 
1.46      www      5148: sub hreflocation {
                   5149:     my ($dir,$file)=@_;
1.460     albertel 5150:     unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
                   5151: 	my $finalpath=filelocation($dir,$file);
                   5152: 	$finalpath=~s-^/home/httpd/html--;
1.462     albertel 5153: 	$finalpath=~s-^/home/(\w+)/public_html/-/~$1/-;
1.460     albertel 5154: 	return $finalpath;
                   5155:     } elsif ($file=~m-^/home-) {
                   5156: 	$file=~s-^/home/httpd/html--;
1.462     albertel 5157: 	$file=~s-^/home/(\w+)/public_html/-/~$1/-;
1.460     albertel 5158: 	return $file;
1.46      www      5159:     }
1.462     albertel 5160:     return $file;
1.465     albertel 5161: }
                   5162: 
                   5163: sub current_machine_domains {
                   5164:     my $hostname=$hostname{$perlvar{'lonHostID'}};
                   5165:     my @domains;
                   5166:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  5167: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 5168: 	if ($hostname eq $name) {
                   5169: 	    push(@domains,$hostdom{$id});
                   5170: 	}
                   5171:     }
                   5172:     return @domains;
                   5173: }
                   5174: 
                   5175: sub current_machine_ids {
                   5176:     my $hostname=$hostname{$perlvar{'lonHostID'}};
                   5177:     my @ids;
                   5178:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  5179: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 5180: 	if ($hostname eq $name) {
                   5181: 	    push(@ids,$id);
                   5182: 	}
                   5183:     }
                   5184:     return @ids;
1.31      www      5185: }
                   5186: 
                   5187: # ------------------------------------------------------------- Declutters URLs
                   5188: 
                   5189: sub declutter {
                   5190:     my $thisfn=shift;
1.479     albertel 5191:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31      www      5192:     $thisfn=~s/^\///;
                   5193:     $thisfn=~s/^res\///;
1.235     www      5194:     $thisfn=~s/\?.+$//;
1.268     www      5195:     return $thisfn;
                   5196: }
                   5197: 
                   5198: # ------------------------------------------------------------- Clutter up URLs
                   5199: 
                   5200: sub clutter {
                   5201:     my $thisfn='/'.&declutter(shift);
1.509     albertel 5202:     unless ($thisfn=~/^\/(uploaded|adm|userfiles|ext|raw|priv|public)\//) { 
1.270     www      5203:        $thisfn='/res'.$thisfn; 
                   5204:     }
1.31      www      5205:     return $thisfn;
1.12      www      5206: }
                   5207: 
                   5208: # -------------------------------------------------------- Escape Special Chars
                   5209: 
                   5210: sub escape {
                   5211:     my $str=shift;
                   5212:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
                   5213:     return $str;
                   5214: }
                   5215: 
                   5216: # ----------------------------------------------------- Un-Escape Special Chars
                   5217: 
                   5218: sub unescape {
                   5219:     my $str=shift;
                   5220:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                   5221:     return $str;
                   5222: }
1.11      www      5223: 
1.415     albertel 5224: sub mod_perl_version {
                   5225:     if (defined($perlvar{'MODPERL2'})) {
                   5226: 	return 2;
                   5227:     }
                   5228:     return 1;
1.436     albertel 5229: }
                   5230: 
                   5231: sub correct_line_ends {
                   5232:     my ($result)=@_;
                   5233:     $$result =~s/\r\n/\n/mg;
                   5234:     $$result =~s/\r/\n/mg;
1.415     albertel 5235: }
1.1       albertel 5236: # ================================================================ Main Program
                   5237: 
1.184     www      5238: sub goodbye {
1.204     albertel 5239:    &logthis("Starting Shut down");
1.443     albertel 5240: #not converted to using infrastruture and probably shouldn't be
1.425     albertel 5241:    &logthis(sprintf("%-20s is %s",'%badServerCache',scalar(%badServerCache)));
1.443     albertel 5242: #converted
1.425     albertel 5243:    &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.443     albertel 5244:    &logthis(sprintf("%-20s is %s",'%homecache',scalar(%homecache)));
1.425     albertel 5245:    &logthis(sprintf("%-20s is %s",'%titlecache',scalar(%titlecache)));
                   5246:    &logthis(sprintf("%-20s is %s",'%courseresdatacache',scalar(%courseresdatacache)));
                   5247: #1.1 only
                   5248:    &logthis(sprintf("%-20s is %s",'%userresdatacache',scalar(%userresdatacache)));
                   5249:    &logthis(sprintf("%-20s is %s",'%usectioncache',scalar(%usectioncache)));
1.440     www      5250:    &logthis(sprintf("%-20s is %s",'%courseresversioncache',scalar(%courseresversioncache)));
                   5251:    &logthis(sprintf("%-20s is %s",'%resversioncache',scalar(%resversioncache)));
1.184     www      5252:    &flushcourselogs();
                   5253:    &logthis("Shutting down");
1.362     albertel 5254:    return DONE;
1.184     www      5255: }
                   5256: 
1.179     www      5257: BEGIN {
1.228     harris41 5258: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
1.195     www      5259:     unless ($readit) {
1.217     harris41 5260: {
1.448     albertel 5261:     open(my $config,"</etc/httpd/conf/loncapa.conf");
1.217     harris41 5262: 
                   5263:     while (my $configline=<$config>) {
1.484     albertel 5264:         if ($configline=~/\S/ && $configline =~ /^[^\#]*PerlSetVar/) {
1.1       albertel 5265: 	   my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
1.8       www      5266:            chomp($varvalue);
1.1       albertel 5267:            $perlvar{$varname}=$varvalue;
                   5268:         }
                   5269:     }
1.448     albertel 5270:     close($config);
1.1       albertel 5271: }
1.227     harris41 5272: {
1.448     albertel 5273:     open(my $config,"</etc/httpd/conf/loncapa_apache.conf");
1.227     harris41 5274: 
                   5275:     while (my $configline=<$config>) {
                   5276:         if ($configline =~ /^[^\#]*PerlSetVar/) {
                   5277: 	   my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
                   5278:            chomp($varvalue);
                   5279:            $perlvar{$varname}=$varvalue;
                   5280:         }
                   5281:     }
1.448     albertel 5282:     close($config);
1.227     harris41 5283: }
1.1       albertel 5284: 
1.327     albertel 5285: # ------------------------------------------------------------ Read domain file
                   5286: {
                   5287:     %domaindescription = ();
                   5288:     %domain_auth_def = ();
                   5289:     %domain_auth_arg_def = ();
1.448     albertel 5290:     my $fh;
                   5291:     if (open($fh,"<".$Apache::lonnet::perlvar{'lonTabDir'}.'/domain.tab')) {
1.327     albertel 5292:        while (<$fh>) {
1.390     matthew  5293:            next if (/^(\#|\s*$)/);
                   5294: #           next if /^\#/;
1.327     albertel 5295:            chomp;
1.403     www      5296:            my ($domain, $domain_description, $def_auth, $def_auth_arg,
                   5297: 	       $def_lang, $city, $longi, $lati) = split(/:/,$_);
                   5298: 	   $domain_auth_def{$domain}=$def_auth;
1.327     albertel 5299:            $domain_auth_arg_def{$domain}=$def_auth_arg;
1.403     www      5300: 	   $domaindescription{$domain}=$domain_description;
                   5301: 	   $domain_lang_def{$domain}=$def_lang;
                   5302: 	   $domain_city{$domain}=$city;
                   5303: 	   $domain_longi{$domain}=$longi;
                   5304: 	   $domain_lati{$domain}=$lati;
                   5305: 
1.448     albertel 5306:  #         &logthis("Domain.tab: $domain, $domain_auth_def{$domain}, $domain_auth_arg_def{$domain},$domaindescription{$domain}");
1.327     albertel 5307: #          &logthis("Domain.tab: $domain ".$domaindescription{$domain} );
1.448     albertel 5308: 	}
1.327     albertel 5309:     }
1.448     albertel 5310:     close ($fh);
1.327     albertel 5311: }
                   5312: 
                   5313: 
1.1       albertel 5314: # ------------------------------------------------------------- Read hosts file
                   5315: {
1.448     albertel 5316:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
1.1       albertel 5317: 
                   5318:     while (my $configline=<$config>) {
1.303     matthew  5319:        next if ($configline =~ /^(\#|\s*$)/);
1.154     www      5320:        chomp($configline);
1.245     www      5321:        my ($id,$domain,$role,$name,$ip,$domdescr)=split(/:/,$configline);
1.252     albertel 5322:        if ($id && $domain && $role && $name && $ip) {
                   5323: 	 $hostname{$id}=$name;
                   5324: 	 $hostdom{$id}=$domain;
                   5325: 	 $hostip{$id}=$ip;
1.300     albertel 5326: 	 $iphost{$ip}=$id;
1.252     albertel 5327: 	 if ($role eq 'library') { $libserv{$id}=$name; }
1.245     www      5328:        }
1.1       albertel 5329:     }
1.448     albertel 5330:     close($config);
1.1       albertel 5331: }
                   5332: 
                   5333: # ------------------------------------------------------ Read spare server file
                   5334: {
1.448     albertel 5335:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 5336: 
                   5337:     while (my $configline=<$config>) {
                   5338:        chomp($configline);
1.284     matthew  5339:        if ($configline) {
1.1       albertel 5340:           $spareid{$configline}=1;
                   5341:        }
                   5342:     }
1.448     albertel 5343:     close($config);
1.1       albertel 5344: }
1.11      www      5345: # ------------------------------------------------------------ Read permissions
                   5346: {
1.448     albertel 5347:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11      www      5348: 
                   5349:     while (my $configline=<$config>) {
1.448     albertel 5350: 	chomp($configline);
                   5351: 	if ($configline) {
                   5352: 	    my ($role,$perm)=split(/ /,$configline);
                   5353: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   5354: 	}
1.11      www      5355:     }
1.448     albertel 5356:     close($config);
1.11      www      5357: }
                   5358: 
                   5359: # -------------------------------------------- Read plain texts for permissions
                   5360: {
1.448     albertel 5361:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      5362: 
                   5363:     while (my $configline=<$config>) {
1.448     albertel 5364: 	chomp($configline);
                   5365: 	if ($configline) {
                   5366: 	    my ($short,$plain)=split(/:/,$configline);
                   5367: 	    if ($plain ne '') { $prp{$short}=$plain; }
                   5368: 	}
1.135     www      5369:     }
1.448     albertel 5370:     close($config);
1.135     www      5371: }
                   5372: 
                   5373: # ---------------------------------------------------------- Read package table
                   5374: {
1.448     albertel 5375:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135     www      5376: 
                   5377:     while (my $configline=<$config>) {
1.483     albertel 5378: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 5379: 	chomp($configline);
                   5380: 	my ($short,$plain)=split(/:/,$configline);
                   5381: 	my ($pack,$name)=split(/\&/,$short);
                   5382: 	if ($plain ne '') {
                   5383: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   5384: 	    $packagetab{$short}=$plain; 
                   5385: 	}
1.11      www      5386:     }
1.448     albertel 5387:     close($config);
1.329     matthew  5388: }
                   5389: 
                   5390: # ------------- set up temporary directory
                   5391: {
                   5392:     $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
                   5393: 
1.11      www      5394: }
                   5395: 
1.71      www      5396: %metacache=();
1.185     www      5397: 
1.281     www      5398: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      5399: $dumpcount=0;
1.22      www      5400: 
1.163     harris41 5401: &logtouch();
1.12      www      5402: &logthis('<font color=yellow>INFO: Read configuration</font>');
1.195     www      5403: $readit=1;
                   5404: }
1.1       albertel 5405: }
1.179     www      5406: 
1.1       albertel 5407: 1;
1.191     harris41 5408: __END__
                   5409: 
1.243     albertel 5410: =pod
                   5411: 
1.191     harris41 5412: =head1 NAME
                   5413: 
1.243     albertel 5414: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 5415: 
                   5416: =head1 SYNOPSIS
                   5417: 
1.243     albertel 5418: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 5419: 
                   5420:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   5421: 
1.243     albertel 5422: Common parameters:
                   5423: 
                   5424: =over 4
                   5425: 
                   5426: =item *
                   5427: 
                   5428: $uname : an internal username (if $cname expecting a course Id specifically)
                   5429: 
                   5430: =item *
                   5431: 
                   5432: $udom : a domain (if $cdom expecting a course's domain specifically)
                   5433: 
                   5434: =item *
                   5435: 
                   5436: $symb : a resource instance identifier
                   5437: 
                   5438: =item *
                   5439: 
                   5440: $namespace : the name of a .db file that contains the data needed or
                   5441: being set.
                   5442: 
                   5443: =back
                   5444: 
1.394     bowersj2 5445: =head1 OVERVIEW
1.191     harris41 5446: 
1.394     bowersj2 5447: lonnet provides subroutines which interact with the
                   5448: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   5449: about classes, users, and resources.
1.243     albertel 5450: 
                   5451: For many of these objects you can also use this to store data about
                   5452: them or modify them in various ways.
1.191     harris41 5453: 
1.394     bowersj2 5454: =head2 Symbs
1.191     harris41 5455: 
1.394     bowersj2 5456: To identify a specific instance of a resource, LON-CAPA uses symbols
                   5457: or "symbs"X<symb>. These identifiers are built from the URL of the
                   5458: map, the resource number of the resource in the map, and the URL of
                   5459: the resource itself. The latter is somewhat redundant, but might help
                   5460: if maps change.
                   5461: 
                   5462: An example is
                   5463: 
                   5464:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   5465: 
                   5466: The respective map entry is
                   5467: 
                   5468:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   5469:   title="Problem 2">
                   5470:  </resource>
                   5471: 
                   5472: Symbs are used by the random number generator, as well as to store and
                   5473: restore data specific to a certain instance of for example a problem.
                   5474: 
                   5475: =head2 Storing And Retrieving Data
                   5476: 
                   5477: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   5478: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   5479: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   5480: is is the non-critical message twin of cstore. These functions are for
                   5481: handlers to store a perl hash to a user's permanent data space in an
                   5482: easy manner, and to retrieve it again on another call. It is expected
                   5483: that a handler would use this once at the beginning to retrieve data,
                   5484: and then again once at the end to send only the new data back.
                   5485: 
                   5486: The data is stored in the user's data directory on the user's
                   5487: homeserver under the ID of the course.
                   5488: 
                   5489: The hash that is returned by restore will have all of the previous
                   5490: value for all of the elements of the hash.
                   5491: 
                   5492: Example:
                   5493: 
                   5494:  #creating a hash
                   5495:  my %hash;
                   5496:  $hash{'foo'}='bar';
                   5497: 
                   5498:  #storing it
                   5499:  &Apache::lonnet::cstore(\%hash);
                   5500: 
                   5501:  #changing a value
                   5502:  $hash{'foo'}='notbar';
                   5503: 
                   5504:  #adding a new value
                   5505:  $hash{'bar'}='foo';
                   5506:  &Apache::lonnet::cstore(\%hash);
                   5507: 
                   5508:  #retrieving the hash
                   5509:  my %history=&Apache::lonnet::restore();
                   5510: 
                   5511:  #print the hash
                   5512:  foreach my $key (sort(keys(%history))) {
                   5513:    print("\%history{$key} = $history{$key}");
                   5514:  }
                   5515: 
                   5516: Will print out:
1.191     harris41 5517: 
1.394     bowersj2 5518:  %history{1:foo} = bar
                   5519:  %history{1:keys} = foo:timestamp
                   5520:  %history{1:timestamp} = 990455579
                   5521:  %history{2:bar} = foo
                   5522:  %history{2:foo} = notbar
                   5523:  %history{2:keys} = foo:bar:timestamp
                   5524:  %history{2:timestamp} = 990455580
                   5525:  %history{bar} = foo
                   5526:  %history{foo} = notbar
                   5527:  %history{timestamp} = 990455580
                   5528:  %history{version} = 2
                   5529: 
                   5530: Note that the special hash entries C<keys>, C<version> and
                   5531: C<timestamp> were added to the hash. C<version> will be equal to the
                   5532: total number of versions of the data that have been stored. The
                   5533: C<timestamp> attribute will be the UNIX time the hash was
                   5534: stored. C<keys> is available in every historical section to list which
                   5535: keys were added or changed at a specific historical revision of a
                   5536: hash.
                   5537: 
                   5538: B<Warning>: do not store the hash that restore returns directly. This
                   5539: will cause a mess since it will restore the historical keys as if the
                   5540: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 5541: 
1.394     bowersj2 5542: Calling convention:
1.191     harris41 5543: 
1.394     bowersj2 5544:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
                   5545:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191     harris41 5546: 
1.394     bowersj2 5547: For more detailed information, see lonnet specific documentation.
1.191     harris41 5548: 
1.394     bowersj2 5549: =head1 RETURN MESSAGES
1.191     harris41 5550: 
1.394     bowersj2 5551: =over 4
1.191     harris41 5552: 
1.394     bowersj2 5553: =item * B<con_lost>: unable to contact remote host
1.191     harris41 5554: 
1.394     bowersj2 5555: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   5556: when the connection is brought back up
1.191     harris41 5557: 
1.394     bowersj2 5558: =item * B<con_failed>: unable to contact remote host and unable to save message
                   5559: for later delivery
1.191     harris41 5560: 
1.394     bowersj2 5561: =item * B<error:>: an error a occured, a description of the error follows the :
1.191     harris41 5562: 
1.394     bowersj2 5563: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 5564: that was requested
1.191     harris41 5565: 
1.243     albertel 5566: =back
1.191     harris41 5567: 
1.243     albertel 5568: =head1 PUBLIC SUBROUTINES
1.191     harris41 5569: 
1.243     albertel 5570: =head2 Session Environment Functions
1.191     harris41 5571: 
1.243     albertel 5572: =over 4
1.191     harris41 5573: 
1.394     bowersj2 5574: =item * 
                   5575: X<appenv()>
                   5576: B<appenv(%hash)>: the value of %hash is written to
                   5577: the user envirnoment file, and will be restored for each access this
                   5578: user makes during this session, also modifies the %ENV for the current
                   5579: process
1.191     harris41 5580: 
                   5581: =item *
1.394     bowersj2 5582: X<delenv()>
                   5583: B<delenv($regexp)>: removes all items from the session
                   5584: environment file that matches the regular expression in $regexp. The
                   5585: values are also delted from the current processes %ENV.
1.191     harris41 5586: 
1.243     albertel 5587: =back
                   5588: 
                   5589: =head2 User Information
1.191     harris41 5590: 
1.243     albertel 5591: =over 4
1.191     harris41 5592: 
                   5593: =item *
1.394     bowersj2 5594: X<queryauthenticate()>
                   5595: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 5596: authentication scheme
                   5597: 
                   5598: =item *
1.394     bowersj2 5599: X<authenticate()>
                   5600: B<authenticate($uname,$upass,$udom)>: try to
                   5601: authenticate user from domain's lib servers (first use the current
                   5602: one). C<$upass> should be the users password.
1.191     harris41 5603: 
                   5604: =item *
1.394     bowersj2 5605: X<homeserver()>
                   5606: B<homeserver($uname,$udom)>: find the server which has
                   5607: the user's directory and files (there must be only one), this caches
                   5608: the answer, and also caches if there is a borken connection.
1.191     harris41 5609: 
                   5610: =item *
1.394     bowersj2 5611: X<idget()>
                   5612: B<idget($udom,@ids)>: find the usernames behind a list of IDs
                   5613: (IDs are a unique resource in a domain, there must be only 1 ID per
                   5614: username, and only 1 username per ID in a specific domain) (returns
                   5615: hash: id=>name,id=>name)
1.191     harris41 5616: 
                   5617: =item *
1.394     bowersj2 5618: X<idrget()>
                   5619: B<idrget($udom,@unames)>: find the IDs behind a list of
                   5620: usernames (returns hash: name=>id,name=>id)
1.191     harris41 5621: 
                   5622: =item *
1.394     bowersj2 5623: X<idput()>
                   5624: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191     harris41 5625: 
                   5626: =item *
1.394     bowersj2 5627: X<rolesinit()>
                   5628: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243     albertel 5629: 
                   5630: =item *
1.394     bowersj2 5631: X<usection()>
                   5632: B<usection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 5633: course $cname, return section name/number or '' for "not in course"
                   5634: and '-1' for "no section"
                   5635: 
                   5636: =item *
1.394     bowersj2 5637: X<userenvironment()>
                   5638: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 5639: passed in @what from the requested user's environment, returns a hash
                   5640: 
                   5641: =back
                   5642: 
                   5643: =head2 User Roles
                   5644: 
                   5645: =over 4
                   5646: 
                   5647: =item *
                   5648: 
                   5649: allowed($priv,$uri) : check for a user privilege; returns codes for allowed
                   5650: actions
                   5651:  F: full access
                   5652:  U,I,K: authentication modes (cxx only)
                   5653:  '': forbidden
                   5654:  1: user needs to choose course
                   5655:  2: browse allowed
                   5656: 
                   5657: =item *
                   5658: 
                   5659: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
                   5660: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
                   5661: and course level
                   5662: 
                   5663: =item *
                   5664: 
                   5665: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
                   5666: explanation of a user role term
                   5667: 
                   5668: =back
                   5669: 
                   5670: =head2 User Modification
                   5671: 
                   5672: =over 4
                   5673: 
                   5674: =item *
                   5675: 
                   5676: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
                   5677: user for the level given by URL.  Optional start and end dates (leave empty
                   5678: string or zero for "no date")
1.191     harris41 5679: 
                   5680: =item *
                   5681: 
1.243     albertel 5682: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   5683: change a users, password, possible return values are: ok,
                   5684: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   5685: refused
1.191     harris41 5686: 
                   5687: =item *
                   5688: 
1.243     albertel 5689: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 5690: 
                   5691: =item *
                   5692: 
1.243     albertel 5693: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) : 
                   5694: modify user
1.191     harris41 5695: 
                   5696: =item *
                   5697: 
1.286     matthew  5698: modifystudent
                   5699: 
                   5700: modify a students enrollment and identification information.
                   5701: The course id is resolved based on the current users environment.  
                   5702: This means the envoking user must be a course coordinator or otherwise
                   5703: associated with a course.
                   5704: 
1.297     matthew  5705: This call is essentially a wrapper for lonnet::modifyuser and
                   5706: lonnet::modify_student_enrollment
1.286     matthew  5707: 
                   5708: Inputs: 
                   5709: 
                   5710: =over 4
                   5711: 
                   5712: =item B<$udom> Students loncapa domain
                   5713: 
                   5714: =item B<$uname> Students loncapa login name
                   5715: 
                   5716: =item B<$uid> Students id/student number
                   5717: 
                   5718: =item B<$umode> Students authentication mode
                   5719: 
                   5720: =item B<$upass> Students password
                   5721: 
                   5722: =item B<$first> Students first name
                   5723: 
                   5724: =item B<$middle> Students middle name
                   5725: 
                   5726: =item B<$last> Students last name
                   5727: 
                   5728: =item B<$gene> Students generation
                   5729: 
                   5730: =item B<$usec> Students section in course
                   5731: 
                   5732: =item B<$end> Unix time of the roles expiration
                   5733: 
                   5734: =item B<$start> Unix time of the roles start date
                   5735: 
                   5736: =item B<$forceid> If defined, allow $uid to be changed
                   5737: 
                   5738: =item B<$desiredhome> server to use as home server for student
                   5739: 
                   5740: =back
1.297     matthew  5741: 
                   5742: =item *
                   5743: 
                   5744: modify_student_enrollment
                   5745: 
                   5746: Change a students enrollment status in a class.  The environment variable
                   5747: 'role.request.course' must be defined for this function to proceed.
                   5748: 
                   5749: Inputs:
                   5750: 
                   5751: =over 4
                   5752: 
                   5753: =item $udom, students domain
                   5754: 
                   5755: =item $uname, students name
                   5756: 
                   5757: =item $uid, students user id
                   5758: 
                   5759: =item $first, students first name
                   5760: 
                   5761: =item $middle
                   5762: 
                   5763: =item $last
                   5764: 
                   5765: =item $gene
                   5766: 
                   5767: =item $usec
                   5768: 
                   5769: =item $end
                   5770: 
                   5771: =item $start
                   5772: 
                   5773: =back
                   5774: 
1.191     harris41 5775: 
                   5776: =item *
                   5777: 
1.243     albertel 5778: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   5779: custom role; give a custom role to a user for the level given by URL.  Specify
                   5780: name and domain of role author, and role name
1.191     harris41 5781: 
                   5782: =item *
                   5783: 
1.243     albertel 5784: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 5785: 
                   5786: =item *
                   5787: 
1.243     albertel 5788: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   5789: 
                   5790: =back
                   5791: 
                   5792: =head2 Course Infomation
                   5793: 
                   5794: =over 4
1.191     harris41 5795: 
                   5796: =item *
                   5797: 
1.243     albertel 5798: coursedescription($courseid) : course description
1.191     harris41 5799: 
                   5800: =item *
                   5801: 
1.243     albertel 5802: courseresdata($coursenum,$coursedomain,@which) : request for current
                   5803: parameter setting for a specific course, @what should be a list of
                   5804: parameters to ask about. This routine caches answers for 5 minutes.
                   5805: 
                   5806: =back
                   5807: 
                   5808: =head2 Course Modification
                   5809: 
                   5810: =over 4
1.191     harris41 5811: 
                   5812: =item *
                   5813: 
1.243     albertel 5814: writecoursepref($courseid,%prefs) : write preferences (environment
                   5815: database) for a course
1.191     harris41 5816: 
                   5817: =item *
                   5818: 
1.243     albertel 5819: createcourse($udom,$description,$url) : make/modify course
                   5820: 
                   5821: =back
                   5822: 
                   5823: =head2 Resource Subroutines
                   5824: 
                   5825: =over 4
1.191     harris41 5826: 
                   5827: =item *
                   5828: 
1.243     albertel 5829: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 5830: 
                   5831: =item *
                   5832: 
1.243     albertel 5833: repcopy($filename) : subscribes to the requested file, and attempts to
                   5834: replicate from the owning library server, Might return
                   5835: HTTP_SERVICE_UNAVAILABLE, HTTP_NOT_FOUND, FORBIDDEN, OK, or
                   5836: HTTP_BAD_REQUEST, also attempts to grab the metadata for the
                   5837: resource. Expects the local filesystem pathname
                   5838: (/home/httpd/html/res/....)
                   5839: 
                   5840: =back
                   5841: 
                   5842: =head2 Resource Information
                   5843: 
                   5844: =over 4
1.191     harris41 5845: 
                   5846: =item *
                   5847: 
1.243     albertel 5848: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
                   5849: a vairety of different possible values, $varname should be a request
                   5850: string, and the other parameters can be used to specify who and what
                   5851: one is asking about.
                   5852: 
                   5853: Possible values for $varname are environment.lastname (or other item
                   5854: from the envirnment hash), user.name (or someother aspect about the
                   5855: user), resource.0.maxtries (or some other part and parameter of a
                   5856: resource)
1.204     albertel 5857: 
                   5858: =item *
                   5859: 
1.243     albertel 5860: directcondval($number) : get current value of a condition; reads from a state
                   5861: string
1.204     albertel 5862: 
                   5863: =item *
                   5864: 
1.243     albertel 5865: condval($condidx) : value of condition index based on state
1.204     albertel 5866: 
                   5867: =item *
                   5868: 
1.243     albertel 5869: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   5870: resource's metadata, $what should be either a specific key, or either
                   5871: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   5872: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   5873: 
                   5874: this function automatically caches all requests
1.191     harris41 5875: 
                   5876: =item *
                   5877: 
1.243     albertel 5878: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   5879: network of library servers; returns file handle of where SQL and regex results
                   5880: will be stored for query
1.191     harris41 5881: 
                   5882: =item *
                   5883: 
1.243     albertel 5884: symbread($filename) : return symbolic list entry (filename argument optional);
                   5885: returns the data handle
1.191     harris41 5886: 
                   5887: =item *
                   5888: 
1.243     albertel 5889: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
                   5890: a possible symb for the URL in $thisfn, returns a 1 on success, 0 on
                   5891: failure, user must be in a course, as it assumes the existance of the
                   5892: course initi hash, and uses $ENV('request.course.id'}
                   5893: 
1.191     harris41 5894: 
                   5895: =item *
                   5896: 
1.243     albertel 5897: symbclean($symb) : removes versions numbers from a symb, returns the
                   5898: cleaned symb
1.191     harris41 5899: 
                   5900: =item *
                   5901: 
1.243     albertel 5902: is_on_map($uri) : checks if the $uri is somewhere on the current
                   5903: course map, user must be in a course for it to work.
1.191     harris41 5904: 
                   5905: =item *
                   5906: 
1.243     albertel 5907: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 5908: 
                   5909: =item *
                   5910: 
1.243     albertel 5911: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   5912: a random seed, all arguments are optional, if they aren't sent it uses the
                   5913: environment to derive them. Note: if symb isn't sent and it can't get one
                   5914: from &symbread it will use the current time as its return value
1.191     harris41 5915: 
                   5916: =item *
                   5917: 
1.243     albertel 5918: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   5919: unfakeable, receipt
1.191     harris41 5920: 
                   5921: =item *
                   5922: 
1.243     albertel 5923: receipt() : API to ireceipt working off of ENV values; given out to users
1.191     harris41 5924: 
                   5925: =item *
                   5926: 
1.243     albertel 5927: countacc($url) : count the number of accesses to a given URL
1.191     harris41 5928: 
                   5929: =item *
                   5930: 
1.243     albertel 5931: 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 5932: 
                   5933: =item *
                   5934: 
1.243     albertel 5935: 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 5936: 
                   5937: =item *
                   5938: 
1.243     albertel 5939: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 5940: 
                   5941: =item *
                   5942: 
1.243     albertel 5943: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   5944: forcing spreadsheet to reevaluate the resource scores next time.
                   5945: 
                   5946: =back
                   5947: 
                   5948: =head2 Storing/Retreiving Data
                   5949: 
                   5950: =over 4
1.191     harris41 5951: 
                   5952: =item *
                   5953: 
1.243     albertel 5954: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
                   5955: for this url; hashref needs to be given and should be a \%hashname; the
                   5956: remaining args aren't required and if they aren't passed or are '' they will
                   5957: be derived from the ENV
1.191     harris41 5958: 
                   5959: =item *
                   5960: 
1.243     albertel 5961: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
                   5962: uses critical subroutine
1.191     harris41 5963: 
                   5964: =item *
                   5965: 
1.243     albertel 5966: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   5967: all args are optional
1.191     harris41 5968: 
                   5969: =item *
                   5970: 
1.243     albertel 5971: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   5972: works very similar to store/cstore, but all data is stored in a
                   5973: temporary location and can be reset using tmpreset, $storehash should
                   5974: be a hash reference, returns nothing on success
1.191     harris41 5975: 
                   5976: =item *
                   5977: 
1.243     albertel 5978: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   5979: similar to restore, but all data is stored in a temporary location and
                   5980: can be reset using tmpreset. Returns a hash of values on success,
                   5981: error string otherwise.
1.191     harris41 5982: 
                   5983: =item *
                   5984: 
1.243     albertel 5985: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   5986: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 5987: 
                   5988: =item *
                   5989: 
1.243     albertel 5990: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   5991: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 5992: 
                   5993: =item *
                   5994: 
1.243     albertel 5995: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   5996: namesp ($udom and $uname are optional)
1.191     harris41 5997: 
                   5998: =item *
                   5999: 
1.243     albertel 6000: dump($namespace,$udom,$uname,$regexp) : 
                   6001: dumps the complete (or key matching regexp) namespace into a hash
                   6002: ($udom, $uname and $regexp are optional)
1.449     matthew  6003: 
                   6004: =item *
                   6005: 
                   6006: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   6007: $store can be a scalar, an array reference, or if the amount to be 
                   6008: incremented is > 1, a hash reference.
                   6009: 
                   6010: ($udom and $uname are optional)
1.191     harris41 6011: 
                   6012: =item *
                   6013: 
1.243     albertel 6014: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   6015: ($udom and $uname are optional)
1.191     harris41 6016: 
                   6017: =item *
                   6018: 
1.524     raeburn  6019: putstore($namespace,$storehash,$udomain,$uname) : stores hash in namesp
                   6020: keys used in storehash include version information (e.g., 1:$symb:message etc.) as
                   6021: used in records written by &store and retrieved by &restore.  This function 
                   6022: was created for use in editing discussion posts, without incrementing the
                   6023: version number included in the key for a particular post. The colon 
                   6024: separated list of attribute names (e.g., the value associated with the key 
                   6025: 1:keys:$symb) is also generated and passed in the ampersand separated 
                   6026: items sent to lonnet::reply().  
                   6027: 
                   6028: =item *
                   6029: 
1.243     albertel 6030: cput($namespace,$storehash,$udom,$uname) : critical put
                   6031: ($udom and $uname are optional)
1.191     harris41 6032: 
                   6033: =item *
                   6034: 
1.243     albertel 6035: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   6036: reference filled in from namesp (encrypts the return communication)
                   6037: ($udom and $uname are optional)
1.191     harris41 6038: 
                   6039: =item *
                   6040: 
1.243     albertel 6041: log($udom,$name,$home,$message) : write to permanent log for user; use
                   6042: critical subroutine
                   6043: 
                   6044: =back
                   6045: 
                   6046: =head2 Network Status Functions
                   6047: 
                   6048: =over 4
1.191     harris41 6049: 
                   6050: =item *
                   6051: 
                   6052: dirlist($uri) : return directory list based on URI
                   6053: 
                   6054: =item *
                   6055: 
1.243     albertel 6056: spareserver() : find server with least workload from spare.tab
                   6057: 
                   6058: =back
                   6059: 
                   6060: =head2 Apache Request
                   6061: 
                   6062: =over 4
1.191     harris41 6063: 
                   6064: =item *
                   6065: 
1.243     albertel 6066: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   6067: localhost, posts hash
                   6068: 
                   6069: =back
                   6070: 
                   6071: =head2 Data to String to Data
                   6072: 
                   6073: =over 4
1.191     harris41 6074: 
                   6075: =item *
                   6076: 
1.243     albertel 6077: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   6078: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 6079: 
                   6080: =item *
                   6081: 
1.243     albertel 6082: hashref2str($hashref) : convert a hashref into a string complete with
                   6083: escaping and '=' and '&' separators, supports elements that are
                   6084: arrayrefs and hashrefs
1.191     harris41 6085: 
                   6086: =item *
                   6087: 
1.243     albertel 6088: arrayref2str($arrayref) : convert an arrayref into a string complete
                   6089: with escaping and '&' separators, supports elements that are arrayrefs
                   6090: and hashrefs
1.191     harris41 6091: 
                   6092: =item *
                   6093: 
1.243     albertel 6094: str2hash($string) : convert string to hash using unescaping and
                   6095: splitting on '=' and '&', supports elements that are arrayrefs and
                   6096: hashrefs
1.191     harris41 6097: 
                   6098: =item *
                   6099: 
1.243     albertel 6100: str2array($string) : convert string to hash using unescaping and
                   6101: splitting on '&', supports elements that are arrayrefs and hashrefs
                   6102: 
                   6103: =back
                   6104: 
                   6105: =head2 Logging Routines
                   6106: 
                   6107: =over 4
                   6108: 
                   6109: These routines allow one to make log messages in the lonnet.log and
                   6110: lonnet.perm logfiles.
1.191     harris41 6111: 
                   6112: =item *
                   6113: 
1.243     albertel 6114: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 6115: 
                   6116: =item *
                   6117: 
1.243     albertel 6118: logthis() : append message to the normal lonnet.log file, it gets
                   6119: preiodically rolled over and deleted.
1.191     harris41 6120: 
                   6121: =item *
                   6122: 
1.243     albertel 6123: logperm() : append a permanent message to lonnet.perm.log, this log
                   6124: file never gets deleted by any automated portion of the system, only
                   6125: messages of critical importance should go in here.
                   6126: 
                   6127: =back
                   6128: 
                   6129: =head2 General File Helper Routines
                   6130: 
                   6131: =over 4
1.191     harris41 6132: 
                   6133: =item *
                   6134: 
1.481     raeburn  6135: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   6136: (a) files in /uploaded
                   6137:   (i) If a local copy of the file exists - 
                   6138:       compares modification date of local copy with last-modified date for 
                   6139:       definitive version stored on home server for course. If local copy is 
                   6140:       stale, requests a new version from the home server and stores it. 
                   6141:       If the original has been removed from the home server, then local copy 
                   6142:       is unlinked.
                   6143:   (ii) If local copy does not exist -
                   6144:       requests the file from the home server and stores it. 
                   6145:   
                   6146:   If $caller is 'uploadrep':  
                   6147:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   6148:     for request for files originally uploaded via DOCS. 
                   6149:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   6150:   
                   6151:   Otherwise:
                   6152:      This indicates a call from the content generation phase of the request.
                   6153:      -  returns the entire contents of the file or -1.
                   6154:      
                   6155: (b) files in /res
                   6156:    - returns the entire contents of a file or -1; 
                   6157:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 6158: 
                   6159: =item *
                   6160: 
1.243     albertel 6161: filelocation($dir,$file) : returns file system location of a file
                   6162: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   6163: directory that relative $file lookups are to looked in ($dir of /a/dir
                   6164: and a file of ../bob will become /a/bob)
1.191     harris41 6165: 
                   6166: =item *
                   6167: 
                   6168: hreflocation($dir,$file) : returns file system location or a URL; same as
                   6169: filelocation except for hrefs
                   6170: 
                   6171: =item *
                   6172: 
                   6173: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
                   6174: 
1.243     albertel 6175: =back
                   6176: 
                   6177: =head2 HTTP Helper Routines
                   6178: 
                   6179: =over 4
                   6180: 
1.191     harris41 6181: =item *
                   6182: 
                   6183: escape() : unpack non-word characters into CGI-compatible hex codes
                   6184: 
                   6185: =item *
                   6186: 
                   6187: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   6188: 
1.243     albertel 6189: =back
                   6190: 
                   6191: =head1 PRIVATE SUBROUTINES
                   6192: 
                   6193: =head2 Underlying communication routines (Shouldn't call)
                   6194: 
                   6195: =over 4
                   6196: 
                   6197: =item *
                   6198: 
                   6199: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   6200: 
                   6201: =item *
                   6202: 
                   6203: reply() : uses subreply to send a message to remote machine, logs all failures
                   6204: 
                   6205: =item *
                   6206: 
                   6207: critical() : passes a critical message to another server; if cannot
                   6208: get through then place message in connection buffer directory and
                   6209: returns con_delayed, if incapable of saving message, returns
                   6210: con_failed
                   6211: 
                   6212: =item *
                   6213: 
                   6214: reconlonc() : tries to reconnect lonc client processes.
                   6215: 
                   6216: =back
                   6217: 
                   6218: =head2 Resource Access Logging
                   6219: 
                   6220: =over 4
                   6221: 
                   6222: =item *
                   6223: 
                   6224: flushcourselogs() : flush (save) buffer logs and access logs
                   6225: 
                   6226: =item *
                   6227: 
                   6228: courselog($what) : save message for course in hash
                   6229: 
                   6230: =item *
                   6231: 
                   6232: courseacclog($what) : save message for course using &courselog().  Perform
                   6233: special processing for specific resource types (problems, exams, quizzes, etc).
                   6234: 
1.191     harris41 6235: =item *
                   6236: 
                   6237: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   6238: as a PerlChildExitHandler
1.243     albertel 6239: 
                   6240: =back
                   6241: 
                   6242: =head2 Other
                   6243: 
                   6244: =over 4
                   6245: 
                   6246: =item *
                   6247: 
                   6248: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 6249: 
                   6250: =back
                   6251: 
                   6252: =cut

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