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