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

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

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