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